WebApi v2不接受text/plain媒体类型

Rob*_*ott 9 c# jquery internet-explorer-9 asp.net-web-api mediatypeformatter

这个问题从IE9开始,对于POST请求,contentType必须是text/plain,并且application/json将不起作用.

我添加了moonscript并继续使用contentType: text/plain.我还将自定义媒体类型添加到api中,如下面的多种表单所示:

并添加了text/plain媒体类型的插入WebApiConfig

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

// allows 'text/plain' as a supported media type
config.Formatters.Add(new TextMediaTypeFormatter());
Run Code Online (Sandbox Code Playgroud)

但是,当在IE9中发布(使用仿真)时,我仍然收到了 415 Unsupported Media Type

Key Value Response HTTP/1.1 415 Unsupported Media Type

$.ajax({
    type: "POST",
    url: hope_forms.viivApiUrl + 'newsletter',
    contentType: 'text/plain',
    data: JSON.stringify(model),
    success: function (data) {
           .....
    },
    error: function (responseText) {
           console.log(responseText)
           modal.showModal('Something went wrong, please try again.');
   }                    
});
Run Code Online (Sandbox Code Playgroud)

加成:

WebApiConfig如果事情发生故障,这就完全爆发了:

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();

config.EnableSystemDiagnosticsTracing();


//config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

// allows 'text/plain' as a supported media type
config.Formatters.Add(new TextMediaTypeFormatter());
Run Code Online (Sandbox Code Playgroud)

我也改变了ajaxTransport xhr包装使用它:https: //github.com/gfdev/javascript-jquery-transport-xdr


注意:

截至今天,09/21,我已经将我的所有POST请求切换到了GET,但我仍然希望能够解决这些问题POST.

Mat*_*ice 1

根据此 MSDN 博客,我认为您已经遇到了 2014 年出现的奇怪的 XDomainRequest 问题

\n
\n

注意:截至 2014 年,XDomainRequest 似乎根本不发送任何 Content-Type 标头。我不清楚这是什么时候改变的。

\n
\n

这是之前关于该主题的一个问题,它实际上引用了该博客。

\n

您正在使用的 jQuery 扩展的文档也支持了这一点。在 Readme.md 中

\n
\n

XDomainRequest 有一些限制:

\n
    \n
  • 请求中没有 Content-Type 标头
  • \n
\n
\n

因此,如果您检查HttpContext.Request.ContentType,我打赌它将为空/空,在这种情况下您应该能够分配“文本/纯文本”的响应类型并向它的工作原理祈祷。

\n

基本上 IE < 10 对 XDomainRequest(甚至 XDomainRequest 本身)的支持是垃圾。根据我的理解,它基本上已经被抛弃了,IE 10 实现了对 XHR 请求的 CORS 支持

\n