How do I specify if I want JSON or XML in ASP.NET Web API?

后端 未结 3 1760
既然无缘
既然无缘 2021-01-04 11:09

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

3条回答
  •  别那么骄傲
    2021-01-04 11:23

    It is defined by what the calling client (eg the browser or your .NET client) passes in the Accept header:

    Accept: application/json, application/xml, text/json, text/xml

    Will have a preference for JSON (if possible)

    So your client that returns XML needs to set the Accept header to be the above or simply

    Accept: application/json

    should do the trick

提交回复
热议问题