简体   繁体   English

如何 model 将 XML POST 与 WebAPI 中的多个子节点绑定(.net core 3)

[英]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:尝试按照以下步骤操作:

  1. Startup.cs

     services.AddMvc().AddJsonOptions().AddXmlSerializerFormatters();
  2. Model Model

     public class TestDTO { public string[] TestValue { get; set; } }
  3. Action行动

    [Consumes("application/xml")] [Produces("application/xml")] [HttpPost("Test")] public IActionResult Test([FromBody] TestDTO model) { return Ok(model); }
  4. 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.

相关问题 模型绑定不适用于 ASP.NET Core 2 WebAPI 中的 POST 请求 - Model binding is not working on POST request in ASP.NET Core 2 WebAPI 如何在angular6和ASP.NET Core 2 WebAPI中使用TypeScript模型发布FormData - How to post formData with typescript model in angular6 and asp.net core 2 webapi 模型未绑定在 POST 控制器方法 .Net Core MVC 中 - Model doesn't bind in POST controller method .Net Core MVC 如何将 Typescript Map 对象发布到 ASP.Net Core WebAPI? - How to post Typescript Map object to ASP.Net Core WebAPI? WebApi XML反序列化-具有多个子节点的节点未正确反序列化为子节点对象的数组 - WebApi XML Deserialization - Node with multiple child nodes is not correctly deserialized as array of childnode objects 如何将 XML 子节点添加到多个父节点 - How to add XML child nodes to multiple parent nodes Linq到XML:将子节点绑定到DataList - Linq to XML: Bind child nodes to DataList ASP .NET Core Webapi父子关系? - ASP .NET Core Webapi Parent - Child relation? 如何在 ASP.NET Core 中创建多个 WebApi 端点 - How to create multiple WebApi enpoints in ASP.NET Core 单元测试.Net CORE 2.0 WebAPI - 模拟(MOQ)HTTP POST与文本流体(不是模型) - Unit Testing .Net CORE 2.0 WebAPI - Mock (MOQ) HTTP POST with text stream body (not model)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM