【发布时间】:2015-04-01 04:48:15
【问题描述】:
这是我的 api 操作:
[HttpGet]
public IHttpActionResult Get()
{
HttpContext.Current.Response.ContentType = "text/xml";
string foo = GetXmlString();
return Ok(foo);
}
我希望当相关请求发生时响应可以返回“text/xml”类型。如果我什么都不做(使用默认配置),如果我做了以下配置,则返回类型将是“application/xml”:
var applicationXml = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(_ => _.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(applicationXml);
var textXml = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(_ => _.MediaType == "text/xml");
if (textXml == null)
{
config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
}
它将改用“application/json”。但我不想删除默认媒体类型“application/json”。
为什么不能按预期应用操作级别配置,而是使用全局配置?我对这种行为感到很困惑。
【问题讨论】:
标签: c# asp.net xml json asp.net-web-api