如何指定,如果我想的ASP.NET Web API JSON或XML? [英] How do I specify if I want JSON or XML in ASP.NET Web API?
问题描述
我已经写了两个的ASP.NET Web API,这个星期都包含一个简单的控制器应用;我已经调整各领他们的工作,包括异常等,但它们的配置几乎相同,据我所看到的。
I've written two ASP.NET Web API applications this week which both contain a single simple controller; I've tweaked each to get them to work, include exceptions, and so on but they are configured almost the same as far as I can see.
其中一个返回JSON和其他收益的XML。我想他们两个返回JSON!
One of these returns JSON and the other returns XML. I want them both to return JSON!
我已经看了一些可能控制这种行为,但我有没有喜悦的配置。哪里是控制使用的的ASP.NET Web API序列化类型的设置?
I've looked for some configuration which might control this behaviour but I'm having no joy. Where is the setting which controls the serialization type used by the ASP.NET Web API?
推荐答案
这是由什么调用客户端(如浏览器或.NET客户端)通过在接受头定义的:
It is defined by what the calling client (eg the browser or your .NET client) passes in the Accept header:
接受:应用/ JSON,应用程序/ XML,文本/ JSON,为text / xml
Accept: application/json, application/xml, text/json, text/xml
将有preference的JSON(如果可能)
Will have a preference for JSON (if possible)
所以,你的客户端返回XML需要设置接受
头是高于或干脆
So your client that returns XML needs to set the Accept
header to be the above or simply
接受:应用/ JSON
Accept: application/json
应该做的伎俩
这篇关于如何指定,如果我想的ASP.NET Web API JSON或XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!