Can I change a parameter name in a C# WebMethod?

我们两清 提交于 2019-12-11 03:43:24

问题


Is it possible for a C# WebMethod to accept a different parameter name than its client sends?

For example, given a client sending this message:

<?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>

Can the existing WebMethod be rewritten to accommodate different argument names? Something like this?

[WebMethod]
public string GetStatus(
    [MessageParameter(Name = "Arg1")] string orderId, 
    [MessageParameter(Name = "Arg2")] string typeId)

回答1:


You should be able to do it with the XmlElementAttribute, e.g:

[WebMethod]
public string GetStatus(
[XmlElementAttribute(ElementName = "Arg1")] string orderId, 
[XmlElementAttribute(ElementName = "Arg2")] string typeId)


来源:https://stackoverflow.com/questions/24997838/can-i-change-a-parameter-name-in-a-c-sharp-webmethod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!