[英]How to model bind XML POST with multiple child nodes in WebAPI (.net core 3)
I am creating a new WebAPI in .NET core 3 which needs to consume XML data POSTed to it.我正在 .NET 核心 3 中创建一个新的 WebAPI,它需要消耗发布给它的 XML 数据。 I am able to modelbind an XML document with a single child element, however I cannot modelbind multiple instances of the same child element (equivalent to JSON modelbinding with List or IEnumerable)
我能够将 XML 文档与单个子元素进行模型绑定,但是我无法对同一子元素的多个实例进行模型绑定(相当于 JSON 与 List 或 IEnumerable 的模型绑定)
The application is running .NET Core 3 version 3.0.100.该应用程序正在运行 .NET Core 3 版本 3.0.100。 I have tried to use List and IEnumerable without result.
我尝试使用 List 和 IEnumerable 没有结果。 On the XML document I have tried some variations including sending multiple XML documents in the same post request.
在 XML 文档上,我尝试了一些变体,包括在同一个发布请求中发送多个 XML 文档。
My Controller:我的 Controller:
[ApiController]
[Route("[controller]")]
public class RenewalNoticeJobController : ControllerBase
{
[Consumes("application/xml")]
[Produces("application/xml")]
[HttpPost("Test")]
public IActionResult Test([FromBody] IEnumerable<TestDTO> model)
{
return Ok(model);
}
}
My DTO/Model:我的 DTO/型号:
public class TestDTO
{
public string TestValue {get;set;}
}
My XML data sent via POSTman我的 XML 数据通过 POSTman 发送
<TestDTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TestValue>TestValue 1</TestValue>
<TestValue>TestValue 2</TestValue>
</TestDTO>
The error message I am receiving when using IEnumerable:使用 IEnumerable 时收到的错误消息:
<problem xmlns="urn:ietf:rfc:7807">
<status>415</status>
<title>Unsupported Media Type</title>
<type>https://tools.ietf.org/html/rfc7231#section-6.5.13</type>
<traceId>|1a4dfac4-4f43b1e42224c38a.</traceId>
</problem>
And when using List:使用列表时:
<problem xmlns="urn:ietf:rfc:7807">
<status>400</status>
<title>One or more validation errors occurred.</title>
<type>https://tools.ietf.org/html/rfc7231#section-6.5.1</type>
<traceId>|d6416e-497079dbab2aeefc.</traceId>
<MVC-Errors>
<MVC-Empty>An error occurred while deserializing input data.</MVC-Empty>
</MVC-Errors>
</problem>
Update: I have updated the question to be more clear.更新:我已将问题更新为更清楚。 What I am trying to modelbind one or more instances of the TestDTO model (equivalent to IEnumerable or List.
我试图对一个或多个 TestDTO model 实例(相当于 IEnumerable 或 List.
There are two issues, one is you need to add xml formatter and another is your xml and model are not sync.有两个问题,一个是您需要添加 xml 格式化程序,另一个是您的 xml 和 model 不同步。
Try follow steps below:尝试按照以下步骤操作:
Startup.cs
services.AddMvc().AddJsonOptions().AddXmlSerializerFormatters();
Model Model
public class TestDTO { public string[] TestValue { get; set; } }
Action行动
[Consumes("application/xml")] [Produces("application/xml")] [HttpPost("Test")] public IActionResult Test([FromBody] TestDTO model) { return Ok(model); }
Request要求
<TestDTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <TestValue> <string>T1</string> <string>T</string> </TestValue> </TestDTO>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.