我可以在C#WebMethod中更改参数名称吗?

Chr*_*uer 1 .net parameters asmx webmethod

C#WebMethod是否可以接受与其客户端发送的不同的参数名称?

例如,给定客户端发送此消息:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <GetStatus xmlns="http://example.com/">
            <Arg1>5</Arg1>
            <Arg2>3</Arg2>
        </GetStatus>
    </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

是否可以重写现有的WebMethod以适应不同的参数名称?像这样的东西?

[WebMethod]
public string GetStatus(
    [MessageParameter(Name = "Arg1")] string orderId, 
    [MessageParameter(Name = "Arg2")] string typeId)
Run Code Online (Sandbox Code Playgroud)

Ste*_*rce 7

您应该可以使用XmlElementAttribute来完成它,例如:

[WebMethod]
public string GetStatus(
[XmlElementAttribute(ElementName = "Arg1")] string orderId, 
[XmlElementAttribute(ElementName = "Arg2")] string typeId)
Run Code Online (Sandbox Code Playgroud)

  • 你刚刚救了我很多或屁股疼,谢谢伙计。我不得不使用一个实现不佳的 asmx SOAP web 服务,该服务具有不一致的参数大小写,我不想通过我的应用程序传播。这让我可以改造门上的外壳,然后从里面处理明智的外壳。这篇文章应该被标记为答案。 (2认同)