在单个操作上启用多个 HTTP 方法?

2023-11-24

我有一个操作合同(如下),我想允许对其进行 GET 和 POST 请求。我怎样才能告诉WCF接受单个OperationContract的两种类型的请求?

[OperationContract,
WebInvoke(Method="POST",
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml,
    ResponseFormat = WebMessageFormat.Xml,
    UriTemplate = "query")]
XElement Query(string qry);

[OperationContract,
WebInvoke(Method="GET",
    BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Xml,
    ResponseFormat = WebMessageFormat.Xml,
    UriTemplate = "query?query={qry}")]
XElement Query(string qry);

如果有人寻找不同的解决方案,

[OperationContract]
[WebInvoke(Method="*")]
public <> DoWork()
{
     var method = WebOperationContext.Current.IncomingRequest.Method;
     if (method == "POST") return DoPost();
     else if (method == "GET") return DoGet();
     throw new ArgumentException("Method is not supported.");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在单个操作上启用多个 HTTP 方法? 的相关文章

随机推荐