让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系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
随机推荐
- C#之Winform中treeview控件绑定数据库
private DataSet ds; private SqlDataAdapter sqlDataAdapter1; private int maxnodeid; private void Form ...
- JSON中的日期格式化
Json字符串中的日期格式化函数 ConvertJsonDate: function (jd) { var d = new Date(parseInt(jd.replace("/Date(& ...
- 泛型的上限和下限的Demo
Main Class package Comparator.Bean; import java.math.BigDecimal; import java.util.List; import java. ...
- 第3章 C#中的委托和事件
.NET框架中的委托和事件 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 读取当前键值,并赋值给LED
/********************************* 代码功能:读取当前键值,并赋值给LED 使用函数: digitalRead(数字输入端口号); 创作时间:2016*10*07 作 ...
- poj 2060 Taxi Cab Scheme (二分匹配)
Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5710 Accepted: 2393 D ...
- JSON.parse()和JSON.stringify()(转载)
parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...
- AVL树模板
///AVL树模板 typedef struct Node ///树的节点 { int val,data; int h; ///以当前结点为根结点的数的高度 int bf; ///平衡因子(左子树高度 ...
- zlib快速编译脚本
zlib允许使用IDE编译生成dll以及静态库,高版本写一个脚本就能轻松的一键编译生成静态/动态lib以及dll文件 以下是一句话编译批处理脚本 nmake -f win32\Makefile.msc ...
- hp小机定位网卡位置
rad已经被olrad取代 HPUX下定位网卡位置 一台HP小型机,可能配了多块网卡,在系统中以la ...