让ASP.NET Web API支持POST纯文本格式(text/plain)的数据
今天在web api中遇到了这样一个问题,虽然api的参数类型是string,但只能接收post body中json格式的string,不能接收原始string。
web api是这样定义的:
public async Task<HttpResponseMessage> Post(string blogApp, int postId, [FromBody] string body)
{
}
以json格式向web api进行post能成功:
var response = await _httpClient.PostAsJsonAsync(
$"api/blogs/{blogApp}/posts/{postId}/comments",
body);
但以纯文本格式(content-type为text/plain)post,body的值却为空。
var response = await _httpClient.PostAsync(
$"api/blogs/{blogApp}/posts/{postId}/comments",
new StringContent(body)
);
研究后发现,这是由于对于content-type为text/plain的post请求,asp.net web api没有提供对应的MediaTypeFormatter,asp.net web api默认只提供了JsonMediaTypeFormatter与XmlMediaTypeFormatter。
所以要解决这个问题,需要自己实现一个PlainTextTypeFormatter,实现代码如下:
public class PlainTextTypeFormatter : MediaTypeFormatter
{
public PlainTextTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}
public override bool CanReadType(Type type)
{
return type == typeof(string);
}
public override bool CanWriteType(Type type)
{
return type == typeof(string);
}
public override async Task WriteToStreamAsync(Type type, object value,
Stream writeStream, HttpContent content, TransportContext transportContext)
{
using (var sw = new StreamWriter(writeStream))
{
await sw.WriteAsync(value.ToString());
}
}
public override async Task<object> ReadFromStreamAsync(Type type, Stream readStream,
HttpContent content, IFormatterLogger formatterLogger)
{
using (var sr = new StreamReader(readStream))
{
return await sr.ReadToEndAsync();
}
}
}
在上面的实现代码中,解决本文中的问题只需实现CanReadType()与ReadFromStreamAsync()。CanWriteType()与ReadFromStreamAsync()的实现是为了解决另外一个问题,详见:让ASP.NET Web API支持text/plain内容协商 。
让ASP.NET Web API支持POST纯文本格式(text/plain)的数据的更多相关文章
- 通过扩展让ASP.NET Web API支持JSONP
同源策略(Same Origin Policy)的存在导致了"源"自A的脚本只能操作"同源"页面的DOM,"跨源"操作来源于B的页面将会被拒 ...
- 通过扩展让ASP.NET Web API支持W3C的CORS规范
让ASP.NET Web API支持JSONP和W3C的CORS规范是解决"跨域资源共享"的两种途径,在<通过扩展让ASP.NET Web API支持JSONP>中我们 ...
- 通过微软的cors类库,让ASP.NET Web API 支持 CORS
前言:因为公司项目需要搭建一个Web API 的后端,用来传输一些数据以及文件,之前有听过Web API的相关说明,但是真正实现的时候,感觉还是需要挺多知识的,正好今天有空,整理一下这周关于解决COR ...
- 通过扩展让ASP.NET Web API支持W3C的CORS规范(转载)
转载地址:http://www.cnblogs.com/artech/p/cors-4-asp-net-web-api-04.html CORS(Cross-Origin Resource Shari ...
- 让ASP.NET Web API支持$format参数的方法
在不使用OData的情况下,也可以让ASP.NET Web API支持$format参数,只要在WebApiConfig里添加如下三行红色粗体代码即可: using System; using Sys ...
- (转)通过扩展让ASP.NET Web API支持JSONP
原文地址:http://www.cnblogs.com/artech/p/3460544.html 同源策略(Same Origin Policy)的存在导致了“源”自A的脚本只能操作“同源”页面的D ...
- [转]让ASP.NET Web API支持$format参数的方法
本文转自:http://www.cnblogs.com/liuzhendong/p/4228592.html 在不使用OData的情况下,也可以让ASP.NET Web API支持$format参数, ...
- 【ASP.NET Web API教程】5.3 发送HTML表单数据:文件上传与多部分MIME
原文:[ASP.NET Web API教程]5.3 发送HTML表单数据:文件上传与多部分MIME 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面 ...
- 【ASP.NET Web API教程】5.2 发送HTML表单数据:URL编码的表单数据
原文:[ASP.NET Web API教程]5.2 发送HTML表单数据:URL编码的表单数据 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
随机推荐
- 利用 Gulp 处理前端工作流程
最近做项目,因为每次做完后都要手动压缩CSS.JS 等文件,压缩后另存为 *.min.xxx. Less 还要手动输入命令进行编译,调整页面也经常要手动刷新页面看效果,很麻烦,就尝试用 gulp 去处 ...
- hive中order by,sort by, distribute by, cluster by作用以及用法
1. order by Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...
- ajax请求后根据条件进行页面跳转
$.ajx({ url: "@Url.Action("DetectCorporationCompetencyCreated", "DataBase") ...
- 真机在wifi下调试android程序
大家好,最近在学习android程序由于手机接口问题,调试程序的时候老是接触不良而不能正常调试,因此感到相当苦恼,于是在网上查找无线调试android的方法.经过研究和尝试现已成功无线调试程序,方法分 ...
- 快考试了,尽快写完HashTable。
(1)Count Primes 质数(素数):在大于1 的自然数中,除了1和它本身之外,不能被任何其他整数整除. 解题思路:使用一个boolean类型的数组,从i(2) 开始循环,将小于N的i的倍数都 ...
- C 语言 *** glibc detected *** free(): invalid next size (fast): 0x0000000000be1010 ***
. . . . . LZ 今天在写一个 Socket 程序的时候使用 malloc(3) 在堆上动态分配了一个结构体的空间,在使用完之后用 free(3) 函数释放空间的时候报 invalid nex ...
- 【iOS问题】The file “XXX.app” couldn’t be opened because you don’t have permission to view it.
当引入第三方的框架的时候 容易产生以下问题: The file "XXX.app" couldn't be opened because you don't have permis ...
- 新手接触java
第二个程序,求同时被3,5,7整除
- 在将 varchar 值 '1,2,3,4,5,6,7,8' 转换成数据类型 int 时失败。
alter PROCEDURE PrTradingDelete ) AS BEGIN WHERE id in(@id) END GO 执行上面这个存储过程会异常.提示 :在将 varchar 值 '1 ...
- centos6.5 安装cmake 3.3.2
os:centos6.5 cmake版本:3.3.2 安装编译源码所需的工具和库 yum install gcc gcc-c++ ncurses-devel perl 下载cmake 使用wget工具 ...