asp.net mvc - Web API uri to routed and defined action returns 404 -


i don't understand how system.web.http method attributes used. have method logon in auth web api controller (asp.net mvc 4):

public httpresponsemessage logon(string email, string password) {     user user = usersrv.load(email);      if (user != null && user.checkpassword(password))         return this.request.createresponse<user>(httpstatuscode.ok, user);         else         return this.request.createresponse(httpstatuscode.badrequest, "invalid username or password"); } 

the webapiconfig.cs file default:

public static class webapiconfig {     public static void register(httpconfiguration config)     {         config.routes.maphttproute(             name: "defaultapi",             routetemplate: "api/{controller}/{action}/{id}",             defaults: new { id = routeparameter.optional }         );          config.formatters.remove(config.formatters.xmlformatter);     } } 

as is, get method returns 405 method not allowed. put, head , other verbs i've tried. post, returns 404 not found following json:

{ "message": "no http resource found matches request uri 'http://localhost:8080/api/auth/logon'.", "messagedetail": "no action found on controller 'auth' matches request." 

}

if add [httppost] attribute before method definition, exact same results. httpget, of course get requests ok. combination of both attributes doesn't change anything. how come post requests not correctly routed?


edit:

the post request not match because uri http://localhost:8080/api/auth/logon, no query parameter. if set default values email , password parameters, method matches. thought mvc smart enough match request content action parameters. need read content stream find argument values?

it's apparently impossible web api bind post request multiple parameters action. easiest solution send json object , parse it. method looks

[httppost] public httpresponsemessage logon(jobject dto) {     dynamic json = dto;     string email = json.email;     string password = json.password;      user user = usersrv.load(email);      if (user != null && user.checkpassword(password))         return this.request.createresponse<user>(httpstatuscode.ok, user);         else         return this.request.createresponse(httpstatuscode.badrequest, "invalid username or password"); } 

see http://www.west-wind.com/weblog/posts/2012/may/08/passing-multiple-post-parameters-to-web-api-controller-methods
, http://www.west-wind.com/weblog/posts/2012/sep/11/passing-multiple-simple-post-values-to-aspnet-web-api


Comments