POST xml 到 .Net Core Web API
POST xml to .Net Core Web API
我在名为 HomeController
的 .Net Core Web API
控制器上执行此操作:
[HttpPost]
public IActionResult TestMethod([FromForm] DocumentDto xml) {
int abc = 0;
return Ok();
}
和模型 DocumentDto
:
[XmlRoot(ElementName = "document", Namespace = "")]
public class DocumentDto
{
[XmlElement(DataType = "string", ElementName = "id")]
public string Id { get; set; }
[XmlElement(DataType = "string", ElementName = "content")]
public string Content { get; set; }
[XmlElement(DataType = "string", ElementName = "author")]
public string Author { get; set; }
}
我还在 int abc = 0;
上添加了一个断点,我使用 Postman 来点击下面显示的操作
用于请求的xml
如下
<document>
<id>12345</id>
<content>This is a Test</content>
<author>vchan</author>
</document>
但是,在调试过程中,xml
变量具有 null
属性,如下所示
此外,在 Startup.cs 文件中,我包含了 AddXmlSerializerFormatters()
,如下所示:
services
.AddMvc()
.AddXmlSerializerFormatters()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
为什么 xml
没有被解析?
根据您的要求,原始 XML 应该被解析 [FromBody]
而不是 [FromForm]
。
我在名为 HomeController
的 .Net Core Web API
控制器上执行此操作:
[HttpPost]
public IActionResult TestMethod([FromForm] DocumentDto xml) {
int abc = 0;
return Ok();
}
和模型 DocumentDto
:
[XmlRoot(ElementName = "document", Namespace = "")]
public class DocumentDto
{
[XmlElement(DataType = "string", ElementName = "id")]
public string Id { get; set; }
[XmlElement(DataType = "string", ElementName = "content")]
public string Content { get; set; }
[XmlElement(DataType = "string", ElementName = "author")]
public string Author { get; set; }
}
我还在 int abc = 0;
上添加了一个断点,我使用 Postman 来点击下面显示的操作
用于请求的xml
如下
<document>
<id>12345</id>
<content>This is a Test</content>
<author>vchan</author>
</document>
但是,在调试过程中,xml
变量具有 null
属性,如下所示
此外,在 Startup.cs 文件中,我包含了 AddXmlSerializerFormatters()
,如下所示:
services
.AddMvc()
.AddXmlSerializerFormatters()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
为什么 xml
没有被解析?
根据您的要求,原始 XML 应该被解析 [FromBody]
而不是 [FromForm]
。