您的位置:首页 > 编程语言 > ASP

让ASP.NET Web API支持text/plain内容协商

2016-01-07 14:11 721 查看
ASP.NET Web API的内容协商(Content Negotiation)机制的理想情况是这样的:客户端在请求头的Accept字段中指定什么样的MIME类型,Web API服务端就返回对应的MIME类型的内容(响应头的中Content-Type就是Accept中指定的MIME类型)。

而现实情况是,Web API服务端能返回什么MIME类型的响应类型取决于有没有对应这个MIME类型的MediaTypeFormatter。ASP.NET Web API的默认实现中只提供了2种MediaTypeFormatter(我用的Web API版本是5.2)—— XmlMediaTypeFormatter与JsonMediaTypeFormatter。所以,在请求头的Accept中除非指定为application/xml或者application/json,否则指定其它任何MIME,Web API都会返回application/json(这是默认的响应类型)。

今天就被这个现实情况折腾了半天,accept中指定text/plain,Web API总是返回json格式的数据。后来通过网络抓包才发现这个问题。真搞不懂ASP.NET Web API为什么不默认实现一个PlainTextTypeFormatter。

被逼无奈,只能自己实现一个PlainTextTypeFormatter:

继承MediaTypeFormatter

构造函数中添加MediaTypeHeaderValue("text/plain")

重写三个方法:CanReadType(), CanWriteType() 与 WriteToStreamAsync()

完整实现代码如下:

public class PlainTextTypeFormatter : MediaTypeFormatter
{
public PlainTextTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
}

public override bool CanReadType(Type type)
{
return false;
}

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());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: