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_start在global.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)
您尝试做的事情不适用于多线程环境.您无法在每个请求的基础上添加和删除格式化程序集合.这是一种更好的方式来完成你想要的.
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)
再看看这个,并在另一篇文章中找到了你的答案:
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)
它也可以强制接受标头.如果你不总是回来,那么很好的选择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)
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)
| 归档时间: |
|
| 查看次数: |
50491 次 |
| 最近记录: |