c# - 如何让 ASP.NET Core 返回 XML 结果?
问题描述
[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 调用了该操作并传递了此参数“文本值”
预期结果应该是这样的 XML 文件:文本值
实际结果:没有传递值的奇怪 json 结果!https://www.screencast.com/t/uzcEed7ojLe
我尝试了以下解决方案,但没有奏效:
services.AddMvc().AddXmlDataContractSerializerFormatters();
services.AddMvc().AddXmlSerializerFormatters();
解决方案
Try this solution
[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# - 如何使用命令参数运行 Interop.Excel.Application?
- java - Spring Boot - 应用程序在 PRD 中丢失数据库连接
- docker - Docker compose - services.vulcain.ports 包含无效类型,应该是数字,或者对象
- python - 在 Python 中制作具有不同参数值的函数序列?
- mysql - Mysql LOAD DATA IN FILE 像 Oracle Sql Loader 一样丢弃文件
- php - 在 Laminas / Zend 框架中注入 SQL 表 - 这是正确的方法吗?
- node.js - 如何在文件更改事件中从 Angular 中的 ngx-image 裁剪器裁剪后将图像上传到节点
- ckeditor - CKEditor 5 中元素“pre”的支持
- python - 为什么不能直接在熊猫中修改一行的属性
- python - Python脚本在当前目录中有效,但在指定时无效