【发布时间】:2012-10-03 09:11:24
【问题描述】:
我了解 WEB API 使用内容协商来接受 - Content-Type 以返回 json 或 xml。 这还不够好,我需要能够务实地决定是要返回 json 还是 xml。
互联网上充斥着使用 HttpResponseMessage<T> 的过时示例,而 MVC 4 中已不再存在。
tokenResponse response = new tokenResponse();
response.something = "gfhgfh";
if(json)
{
return Request.CreateResponse(HttpStatusCode.OK, response, "application/json");
}
else
{
return Request.CreateResponse(HttpStatusCode.OK, response, "application/xml");
}
如何更改上述代码以使其正常工作?
【问题讨论】: