简体   繁体   English

WCF中URI模板中的附加/可选查询字符串参数

[英]Additional/Optional query string parameters in URI Template in WCF

I have written a simple REST Service in WCF in which I have created 2 method using same URI Template but with different Method(POST and GET). 我在WCF中编写了一个简单的REST服务,其中我使用相同的URI模板但使用不同的方法(POST和GET)创建了2个方法。 For GET method I am also sending additional query parameters as follows: 对于GET方法,我还发送其他查询参数,如下所示:

    [WebInvoke(Method = "POST", UriTemplate = "users")]
    [OperationContract]
    public bool CreateUserAccount(User user)
    {
        //do something
        return restult;
    }

    [WebGet(UriTemplate = "users?userid={userid}&username={userName}")]
    [OperationContract]
    public User GetUser(int userid, string userName)
    {
       // if User ID then 
       //   Get User By UserID
       //else if User Name then 
       //   Get User By User Name
       //if no paramter then do something

    }

when I call CreateUserAccount with method POST it is working fine but when I call GetUser method using GET and sending only one query string parameter(userID or UserName) it is giving error "HTTP Method not allowed" but if send both parameters its wokrs fine. 当我使用方法POST调用CreateUserAccount时,它工作正常但是当我使用GET调用GetUser方法并且只发送一个查询字符串参数(userID或UserName)时,它给出错误“HTTP方法不允许”但是如果发送两个参数它的wokrs很好。

Can anyone help me? 谁能帮我?

不要指定任何可选参数,并使用WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters来访问所有这些参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM