ASP.NET Core ActionContext.Result 返回 XML 数据
使用 ActionContext.Result 返回 XML 数据的方法如下:
public class MyController : ControllerBase
{
public IActionResult MyAction()
{
// 创建 XML 文档
var xmlDoc = new XDocument(
new XElement('Root',
new XElement('Element1', 'Value1'),
new XElement('Element2', 'Value2')
)
);
// 返回 XML 结果
return Content(xmlDoc.ToString(), 'application/xml');
}
}
在这个示例中,首先创建一个 XML 文档,然后使用 Content 方法将 XML 文档转换为字符串,并设置响应的内容类型为 'application/xml'。最后,将该结果返回给客户端。
原文地址: https://www.cveoy.top/t/topic/olDe 著作权归作者所有。请勿转载和采集!