如何根据我的输入强制ASP.NET Web API返回JSON或XML数据?

Raj*_*ran 40 c# asp.net-web-api

我尝试根据输入获取输出XML或JSON数据.我使用了以下的WEB API代码,但无法准确输出.

public string Get(int id)
{
    if (GlobalConfiguration.Configuration.Formatters.XmlFormatter == null)
    {
        GlobalConfiguration.Configuration.Formatters.Add(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
    }
    if (GlobalConfiguration.Configuration.Formatters.JsonFormatter == null)
    {
        GlobalConfiguration.Configuration.Formatters.Add(GlobalConfiguration.Configuration.Formatters.JsonFormatter);
    }
    if (id == 1)
    {
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.JsonFormatter);                
        GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;                
    }
    else
    {
        GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.UseDataContractJsonSerializer = true;
    }
    return "value";
}
Run Code Online (Sandbox Code Playgroud)

小智 78

app_startglobal.asax文件中添加以下代码事件.在API Url中添加查询字符串:

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")));
Run Code Online (Sandbox Code Playgroud)

例如:

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

for json: http://localhost:49533/api/?type=json
Run Code Online (Sandbox Code Playgroud)

  • 它是否适用于HTTP路由,如`[Route("api/{type}/entity")]`? (2认同)

Bad*_*dri 9

您尝试做的事情不适用于多线程环境.您无法在每个请求的基础上添加和删除格式化程序集合.这是一种更好的方式来完成你想要的.

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
    };
}
Run Code Online (Sandbox Code Playgroud)


jam*_*row 8

再看看这个,并在另一篇文章中找到了你的答案:

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);
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*itz 6

它也可以强制接受标头.如果你不总是回来,那么很好的选择HttpResponseMessage's.即

Request.Headers.Add("Accept", "text/json");
return Request.CreateResponse(HttpStatusCode.OK, yourobject);
Run Code Online (Sandbox Code Playgroud)

要么

Request.Headers.Add("Accept", "application/xml");
return new Rss20FeedFormatter(feed);
Run Code Online (Sandbox Code Playgroud)


Jim*_*bro 5

例如application/json,如果您的请求指定了mime类型,那么web api将适当地格式化响应.

如果您尝试手动调试Web api,请使用Fiddler 2等工具指定类型.

本文介绍了这个概念.


Par*_*dox 5

QueryStringMapping` 是一个很好的解决方案,但我需要类型的默认值。

对于 XML:localhost:49533/api/?type=xml

对于 json:localhost:49533/api/

我这样解决这种情况:

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")));
Run Code Online (Sandbox Code Playgroud)