让 ASP.NET Core 8 Web API 接受 SOAP(如 XML)作为输入

问题描述 投票:0回答:1

我正在尝试编写一个接受 SOAP XML 并返回 XML 响应的 ASP.NET Core 8 Web API。

我创建了这个控制器:

[HttpPost]
[Produces("application/xml")]
[Consumes("application/xml")]
public IActionResult Get(TestThing inputXml)
{
    return Ok();
} 

并向

Program.cs
添加了 XML 格式化程序:

builder.Services.AddControllers()
    .AddXmlSerializerFormatters();

这是我的请求类:

[XmlRoot("TestThing", Namespace = "http://www.w3.org/2003/05/soap-envelope/")]
public partial class TestThing
{
    public string? Header { get; set; }
}

如果我使用此 XML 作为输入,效果很好

<TestThing xmlns="http://www.w3.org/2003/05/soap-envelope/">
    <Header>sdfs sdfsd sdfsdfs sdfsdf</Header>
</TestThing>

问题是,根据文档,服务器将要接收的 XML 是这样的:

<soapenv:ETestThing xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/">
    <soapenv:Header>sdfs sdfsd sdfsdfs sdfsdf</soapenv:Header>
</soapenv:ETestThing>

当我尝试发送该 xml 时,出现此错误:

反序列化输入数据时发生错误

是的,

soapenv:
部分似乎有点问题。

我应该怎么做才能正确反序列化数据?我有点困惑:我正在遵循本教程https://www.youtube.com/watch?v=j2r7z4W30AE并且它似乎不需要额外的步骤来接受xml输入(实际上我有一个额外的

[Consumes("application/xml")]
那家伙不需要运行它的代码)但就我而言,似乎缺少了一些东西。

web-services soap asp.net-core-webapi asp.net-core-8
1个回答
0
投票

你的xml是

<soapenv:ETestThing
而不是
<soapenv:TestThing
,所以对应的模型设计应该改为:

[XmlRoot("ETestThing", Namespace = "http://www.w3.org/2003/05/soap-envelope/")]
© www.soinside.com 2019 - 2024. All rights reserved.