简体   繁体   中英

Why is this method not allowed for WebInvoke, but OK for WebGet?

Can someone explain the reason behind this or how it works? IF I do a WebInvoke on the following, it fails (says method not allowed, but if I do a WebGet, it passes). I just want to understand why?

[OperationContract]
[WebGet(UriTemplate = "login/{username}/{password}", ResponseFormat =  
                                                        WebMessageFormat.Json)]
string Login(string username, string password);

The above code, just returns a hard coded string. (No conditional logic)

EDIT: Rewritten somewhat now I've reread the question...

WebInvoke allows you to specify which verb will be allowed, defaulting to POST. WebGet requires the client to be using a GET request. In either case, if the wrong verb is used, you'll get "method is not allowed". You were using the browser, so it was making a GET request, so a normal POST-only WebInvoke would reject it, whereas a WebGet would allow it. You could specify Method="GET" in the WebInvoke attribute declaration to allow GET, of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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