在WEB API(.NET CORE)中忽略SOAP XML参数中的Envelope和Body

时间:2018-01-11 13:05:58

标签: c# .net asp.net-core .net-core asp.net-core-mvc

我正在使用Web API(.NET CORE 2.0),API接收基于SOAP的XML请求。见下文

<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:ns="http://example.com/ns">
   <soapenv:Body>
      <ns:Customer>
       <ns:Id>123</ns:Id>
       <ns:Name>John Brown</ns:Name>
      </ns:Customer>
   </soapenv:Body>
</soapenv:Envelope>

上述XML工作正常,并被web api接受。

控制器:

    [Route("api/Customer")]
    public class CustomerController : Controller
    {
        [Route("Detail")]
        [HttpPost]
        public IActionResult Detail([FromBody]Envelope request)
        {
            return Ok();
        }
    }

请求对象:

 [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
    [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Body Body { get; set; }
}

[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
    [XmlElement(ElementName = "Customer", Namespace = "http://example.com/ns")]
    public Customer Customer { get; set; }
}

[XmlRoot(ElementName = "Customer", Namespace = "http://example.com/ns")]
public class Customer
{
    [XmlElement(ElementName = "Id", Namespace = "http://example.com/ns")]
    public string Id { get; set; }

    [XmlElement(ElementName = "Name", Namespace = "http://example.com/ns")]
    public string Name { get; set; }
}

上面的代码运行正常,我想问一下有没有更好的方法来消除或删除EnvelopeBody类(由XmlRoot和XmlElement修饰)

所以我们只接受我们想要的对象。 每次我创建一个对象参数时,总是需要我创建Envelope和Body类,只是为了满足SOAP xml。

控制器看起来像这样。

[Route("api/Customer")]
public class CustomerController : Controller
{
    [Route("Detail")]
    [HttpPost]
    public IActionResult Detail([FromBody]Customer request)
    {
        return Ok();
    }
}

但仍接受上述XML。

0 个答案:

没有答案