[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 调用了 Action 并传递了这个参数“文本值”
预期 结果应该是这样的 XML 文件:文本值
实际结果:奇怪的 json 结果没有传递值! https://www.screencast.com/t/uzcEed7ojLe
我尝试了以下解决方案但没有奏效:
services.AddMvc().AddXmlDataContractSerializerFormatters();
services.AddMvc().AddXmlSerializerFormatters();
最佳答案
试试这个解决方案
[HttpGet]
[HttpPost]
public ContentResult GetXml(string value)
{
var xml = $"<result><value>{value}</value></result>";
return new ContentResult
{
Content = xml,
ContentType = "application/xml",
StatusCode = 200
};
}
关于c# - 如何让 ASP.NET Core 返回 XML 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59190580/