如何使 ASP.NET 核心 return XML 结果?
How to make ASP.NET Core return XML result?
[HttpGet]
[HttpPost]
public HttpResponseMessage GetXml(string value)
{
var xml = $"<result><value>{value}</value></result>";
return new HttpResponseMessage
{
Content = new StringContent(xml, Encoding.UTF8, "application/xml")
};
}
我使用 Swagger 调用了动作并传递了这个参数 'text value'
预期 结果应该是这样的 XML 文件:文本值
实际结果:奇怪的json结果没有传递值! https://www.screencast.com/t/uzcEed7ojLe
我尝试了以下解决方案但没有奏效:
services.AddMvc().AddXmlDataContractSerializerFormatters();
services.AddMvc().AddXmlSerializerFormatters();
对于ASP.NET核心2+,需要配置XmlDataContractSerializerOutputFormatter
, it could be found from Nuget:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(c =>
{
c.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
});
}
试试这个解决方案
[HttpGet]
[HttpPost]
public ContentResult GetXml(string value)
{
var xml = $"<result><value>{value}</value></result>";
return new ContentResult
{
Content = xml,
ContentType = "application/xml",
StatusCode = 200
};
}
- 您的属性中缺少参数。
- 你可以把produces属性声明成什么类型的输出格式
可以拥抱IActionResult或者直接returnContentResult.
[HttpGet("{value}")]
[Produces("application/xml")]
public IActionResult GetXml(string value)
{
var xml = $"<result><value>{value}</value></result>";
//HttpResponseMessage response = new HttpResponseMessage();
//response.Content = new StringContent(xml, Encoding.UTF8);
//return response;
return new ContentResult{
ContentType = "application/xml",
Content = xml,
StatusCode = 200
};
}
以上会给你
<result>
<value>hello</value>
</result>
您可以参考以下link了解更多关于IActionResult
[HttpGet]
[HttpPost]
public HttpResponseMessage GetXml(string value)
{
var xml = $"<result><value>{value}</value></result>";
return new HttpResponseMessage
{
Content = new StringContent(xml, Encoding.UTF8, "application/xml")
};
}
我使用 Swagger 调用了动作并传递了这个参数 'text value'
预期 结果应该是这样的 XML 文件:文本值
实际结果:奇怪的json结果没有传递值! https://www.screencast.com/t/uzcEed7ojLe
我尝试了以下解决方案但没有奏效:
services.AddMvc().AddXmlDataContractSerializerFormatters();
services.AddMvc().AddXmlSerializerFormatters();
对于ASP.NET核心2+,需要配置XmlDataContractSerializerOutputFormatter
, it could be found from Nuget:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(c =>
{
c.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
});
}
试试这个解决方案
[HttpGet]
[HttpPost]
public ContentResult GetXml(string value)
{
var xml = $"<result><value>{value}</value></result>";
return new ContentResult
{
Content = xml,
ContentType = "application/xml",
StatusCode = 200
};
}
- 您的属性中缺少参数。
- 你可以把produces属性声明成什么类型的输出格式
可以拥抱IActionResult或者直接returnContentResult.
[HttpGet("{value}")] [Produces("application/xml")] public IActionResult GetXml(string value) { var xml = $"<result><value>{value}</value></result>"; //HttpResponseMessage response = new HttpResponseMessage(); //response.Content = new StringContent(xml, Encoding.UTF8); //return response; return new ContentResult{ ContentType = "application/xml", Content = xml, StatusCode = 200 }; }
以上会给你
<result>
<value>hello</value>
</result>
您可以参考以下link了解更多关于IActionResult