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

前端 未结 7 1847
佛祖请我去吃肉
佛祖请我去吃肉 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:08

    QueryStringMapping` is nice solution but I need a default value for type.

    for xml : localhost:49533/api/?type=xml

    for json: localhost:49533/api/

    I solve that situation like that:

    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
    var jSettings = new JsonSerializerSettings();
    
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = jSettings;
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("xml", "true", new MediaTypeHeaderValue("application/xml")));
    
    0 讨论(0)
  • 2020-12-07 13:09

    Looked into this a bit more, and found your answer in another post:

    public HttpResponseMessage Get(int id)
    {
        string content = "value";
    
        if (id == 1)
        {
            return Request.CreateResponse<string>(HttpStatusCode.OK, content, Configuration.Formatters.JsonFormatter);
        }
    
        return Request.CreateResponse<string>(HttpStatusCode.OK, content, Configuration.Formatters.XmlFormatter);
    }
    
    0 讨论(0)
  • 2020-12-07 13:13

    Add the below code app_start event in global.asax file. In API Url add the query string:

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
        new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
    
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
        new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
    

    e.g.:

    for xml : http://localhost:49533/api/?type=xml
    
    for json: http://localhost:49533/api/?type=json
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-07 13:24

    If your request specifies the mime type, for example application/json, then web api will format the response appropriately.

    If you are attempting to debug your web api manually, use a tool like Fiddler 2 to specify the type.

    This article describes the concept.

    0 讨论(0)
  • 2020-12-07 13:25

    What you are trying to do will not work in a multi-threaded environment. You cannot add to and remove from the formatters collection on a per-request basis. Here is a better way of accomplishing what you want.

    public HttpResponseMessage Get(int id)
    {
        Foo foo = new Foo();
        var content = new ObjectContent<Foo>(foo,
                        ((id == 1) ? Configuration.Formatters.XmlFormatter :
                                    Configuration.Formatters.JsonFormatter));
        return new HttpResponseMessage()
        {
             Content = content
        };
    }
    
    0 讨论(0)
提交回复
热议问题