[英]XMl Response from WEB API solution
I have a Web API solution which is configured to respond results always in JSON format as below 我有一个Web API解决方案,其配置为始终以JSON格式响应结果,如下所示
config.Formatters.JsonFormatter.SerializerSettings.Formatting =
Newtonsoft.Json.Formatting.Indented;
But now I have a requirement to respond only one of the API calls with XML Response . 但是现在我需要使用XML Response仅响应一个API调用。 But if I add the XML formatter to the system
但是如果我将XML格式化程序添加到系统中
config.Formatters.XmlFormatter.UseXmlSerializer = true;
then all the API calls get affected. 那么所有的API调用都会受到影响。
I have a XML string hardcoded which I want to give as reponse . 我有一个硬编码的XML字符串,我想作为响应给出。 How can I solve this problem ?
我怎么解决这个问题 ?
You have to use the Сonfiguration.Formatters.XmlFormatter at api level. 您必须在api级别使用Сonfiguration.Formatters.XmlFormatter。 Try below code
试试下面的代码
public IHttpActionResult ApiMethod()
{
...
return Content(HttpStatusCode.OK, Model, Configuration.Formatters.XmlFormatter);
}
Write the below code in your action: 在您的操作中编写以下代码:
Class1 c1 = new Class1();//Your Model class
var content = new ObjectContent<Class1>(c1,
GlobalConfiguration.Configuration.Formatters.XmlFormatter);
return new HttpResponseMessage()
{
Content = content
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.