How to force ASP.NET Web API to return JSON or XML data based on my input?

前端 未结 7 1851
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 12:56

I try to get the output XML or JSON data based on my input. I used the below WEB API code but not able to exact output.

public string Get(int id)
{
    if (G         


        
7条回答
  •  無奈伤痛
    2020-12-07 13:14

    It also works to force the accept headers. Great option if you aren't always returning HttpResponseMessage's. I.e

    Request.Headers.Add("Accept", "text/json");
    return Request.CreateResponse(HttpStatusCode.OK, yourobject);
    

    or

    Request.Headers.Add("Accept", "application/xml");
    return new Rss20FeedFormatter(feed);
    

提交回复
热议问题