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());
}
}
}

让ASP.NET Web API支持text/plain内容协商的更多相关文章

  1. 让ASP.NET Web API支持POST纯文本格式(text/plain)的数据

    今天在web api中遇到了这样一个问题,虽然api的参数类型是string,但只能接收post body中json格式的string,不能接收原始string. web api是这样定义的: pub ...

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

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

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

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

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

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

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

  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参数的方法

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

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

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

  9. 通过扩展让ASP.NET Web API支持JSONP -摘自网络

    同源策略(Same Origin Policy)的存在导致了“源”自A的脚本只能操作“同源”页面的DOM,“跨源”操作来源于B的页面将会被拒绝.同源策略以及跨域资源共享在大部分情况下针对的是Ajax请 ...

随机推荐

  1. codeforces 361 B - Mike and Shortcuts

    原题: Description Recently, Mike was very busy with studying for exams and contests. Now he is going t ...

  2. python语句表达式——黑板客老师课程学习

    1.赋值 多重赋值: a,b=1,2 a,b=’beijing’,’sh’ a,b=’bj’ a,b=(1,2) a,b=[1,2] …… 2.输入输出 输入: raw_input()   原始输入 ...

  3. 如何实现能像windows 窗体一样改变大小的控件 Silverlight

    众所周知,我们可以将鼠标放在windows窗体的边框上,按住鼠标左键改变窗体大小.那么,在silverlight上如何实现呢? 1. 需要将改控件放置在canvas上. 2. 判断鼠标位置,然后将Ar ...

  4. [2015hdu多校联赛补题]hdu5302 Connect the Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...

  5. DNS解析过程

    参考: http://www.maixj.net/ict/dns-chaxun-9208 http://blog.it985.com/8389.html DNS(Domain Name System) ...

  6. C#WebClient常见用法

    System.Net.WebClient.DownloadFile(Uri address, String fileName) namespace:System.Net 参数: address:The ...

  7. The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProv

    在mybatis配置文件config.xml中报错: The content of element type "configuration" must match "(p ...

  8. 关于Datagridview控件用法的一些总结(设置列chicun)

    1. 关于Datagridview控件用法的一些总结:http://www.cnblogs.com/mingjiatang/p/4968049.html

  9. 图解Android Studio导入Eclipse项目源码

    方法/步骤   打开Android Studio,在主页面中选择"File"->"New"->"Import project...&quo ...

  10. java核心知识点学习----多线程并发之线程同步

    1.什么是线程同步? 多线程编程是很有趣的事情,它很容易出现"错误情况",这种情况不是由编码造成的,它是由系统的线程调度造成的,当使用多个线程来访问同一个数据时,很容易出现&quo ...