XML响应
问题描述:
我有被配置为JSON格式如下XML响应
config.Formatters.JsonFormatter.SerializerSettings.Formatting =
Newtonsoft.Json.Formatting.Indented;
总是回复结果的网页API的解决方案,但现在我有一个要求,只有API的一个使用XML电话回应响应。但是,如果我将XML格式化程序添加到系统中,则所有API调用都会受到影响
config.Formatters.XmlFormatter.UseXmlSerializer = true;
然后所有的API调用都会受到影响。
我有一个硬编码的XML字符串,我想给作为响应。 我该如何解决这个问题?
答
您必须在api级别使用Сonfiguration.Formatters.XmlFormatter。尝试下面的代码
public IHttpActionResult ApiMethod()
{
...
return Content(HttpStatusCode.OK, Model, Configuration.Formatters.XmlFormatter);
}
+0
我使用的是相同的 –
+0
但根据你的问题描述,似乎你已经把Formatter放到了Global.asax中。它必须采取行动。 –
答
写下面的代码在你的行动:
Class1 c1 = new Class1();//Your Model class
var content = new ObjectContent<Class1>(c1,
GlobalConfiguration.Configuration.Formatters.XmlFormatter);
return new HttpResponseMessage()
{
Content = content
};
http://stackoverflow.com/questions/14313241/per-route-formatter-configuration-in-web的可能的复制-api – torvin