Optional / Optional Query String Parameters in WCF URI Pattern

I wrote a simple REST service in WCF in which I created 2 methods using the same URI pattern, but with a different method (POST and GET). For the GET method, I also send additional request parameters as follows:

[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 the POST method, it works fine, but when I call the GetUser method using GET and sending only one query string parameter (userID or UserName), it gives the error "HTTP method not allowed", but if both parameters are sent , wokrs great.

Can anybody help me?

+4
source share
1 answer

Do not specify any optional parameters and use WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters to access all of them.

+5
source

Source: https://habr.com/ru/post/1339365/


All Articles