如何更改默认的ASP.NET MVC的Web API的媒体格式? [英] How to change default ASP.NET MVC Web API media formatter?
问题描述
我有一个返回一些产品数据的Web API项目。它正确地协商根据请求的Accept头(JSON / XML)的返回类型。问题是,如果没有指定Accept头返回JSON,但我想它在默认情况下返回XML。如何更改的内容协商默认Global.asax中?
I have a Web API project that returns some product data. It negotiates the return type correctly depending on the Accept header (JSON/XML) of the request. The problem is, if no Accept header is specified it returns JSON, but I want it to return XML by default. How do I change the content negotiation defaults in Global.asax?
推荐答案
经发现<一个解决方案href=\"http://www.strathweb.com/2013/06/supporting-only-json-in-asp-net-web-api-the-right-way/\">http://www.strathweb.com/2013/06/supporting-only-json-in-asp-net-web-api-the-right-way/
在 WebApiConfig.Register加入这个()
:
config.Formatters.Clear();
config.Formatters.Add(new XmlMediaTypeFormatter());
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.Add(new FormUrlEncodedMediaTypeFormatter());
所以默认为XML,第一次格式化,但是API仍然支持JSON如果请求需要它。
So default is XML, the first formatter, but the API still supports JSON if the request asks for it.
这篇关于如何更改默认的ASP.NET MVC的Web API的媒体格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!