今天在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)的数据的更多相关文章

  1. 通过扩展让ASP.NET Web API支持JSONP

    同源策略(Same Origin Policy)的存在导致了"源"自A的脚本只能操作"同源"页面的DOM,"跨源"操作来源于B的页面将会被拒 ...

  2. 通过扩展让ASP.NET Web API支持W3C的CORS规范

    让ASP.NET Web API支持JSONP和W3C的CORS规范是解决"跨域资源共享"的两种途径,在<通过扩展让ASP.NET Web API支持JSONP>中我们 ...

  3. 通过微软的cors类库,让ASP.NET Web API 支持 CORS

    前言:因为公司项目需要搭建一个Web API 的后端,用来传输一些数据以及文件,之前有听过Web API的相关说明,但是真正实现的时候,感觉还是需要挺多知识的,正好今天有空,整理一下这周关于解决COR ...

  4. 通过扩展让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 ...

  5. 让ASP.NET Web API支持$format参数的方法

    在不使用OData的情况下,也可以让ASP.NET Web API支持$format参数,只要在WebApiConfig里添加如下三行红色粗体代码即可: using System; using Sys ...

  6. (转)通过扩展让ASP.NET Web API支持JSONP

    原文地址:http://www.cnblogs.com/artech/p/3460544.html 同源策略(Same Origin Policy)的存在导致了“源”自A的脚本只能操作“同源”页面的D ...

  7. [转]让ASP.NET Web API支持$format参数的方法

    本文转自:http://www.cnblogs.com/liuzhendong/p/4228592.html 在不使用OData的情况下,也可以让ASP.NET Web API支持$format参数, ...

  8. 【ASP.NET Web API教程】5.3 发送HTML表单数据:文件上传与多部分MIME

    原文:[ASP.NET Web API教程]5.3 发送HTML表单数据:文件上传与多部分MIME 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面 ...

  9. 【ASP.NET Web API教程】5.2 发送HTML表单数据:URL编码的表单数据

    原文:[ASP.NET Web API教程]5.2 发送HTML表单数据:URL编码的表单数据 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...

随机推荐

  1. 服务器响应头隐藏X-power-by

    服务器响应头隐藏X-power-by 这个不是Apache或者Nginx输出的,而是由语言解析器或者应用程序框架输出的. 这个值的意义用于告知网站是用何种语言或框架编写的.例如: PHP标准输出是:X ...

  2. Study Emgu VoteForUniqueness

    Recently i was studying Emgu, but find there is a bug in the VoteForUniqueness function in class Fea ...

  3. 开发常用小demo 整理

    pc懒加载   https://github.com/ningmengxs/Lazy_loading 元素滑动 js 效果     https://github.com/ningmengxs/elem ...

  4. 通过Percona XtraBackup添加一个slave

    假设我的master是172.16.15.8,要添加的slave是172.16.15.9 1:在主服务器执行备份操作(172.16.15.8) /usr/bin/innobackupex --comp ...

  5. dubbo 使用总结

    第一步: 安装注册中心Register,这里选择 zookeeper 1.zookeeper下载url:http://zookeeper.apache.org; 2.下载解压完后如下: 3.将zoo_ ...

  6. PHP世纪万年历

    <?  //世纪万年历  #这是唯一的设置-请输入php文件的位置  $file="http://192.168.1.168/php/rl/s2m.php";  //#农历每 ...

  7. PS 制作复印件及盖章效果

    对要处理的部分选定  1.执行 滤镜--杂色--添加杂色 2.执行 滤镜--模糊--高斯模糊 3.ctrl+L 执行 色阶 调整为 满意的效果  4.最后添加想要的颜色 图像--调整--渐变映射 关键 ...

  8. python之路-Day1

    Python 是一门什么样的语言? python是一门动态解释性的强类型定义语言 动态语言:动态类型语言是指在运行期间才去做数据类型检查的语言,也就是说,在用动态类型的语言编程时,永远也不用给任何变量 ...

  9. construction of tuples containing 0 or 1 items

    [construction of tuples containing 0 or 1 items] the syntax has some extra quirks to accommodate the ...

  10. 转 系统级编程语言性能PK

    http://www.solidot.org/story?sid=35754 看了此文,为什么我现在如此看好Rust C/C++已经统治系统编程很久,除了ObjectiveC之外语言都无法获得很高的关 ...