asp.net web api - Method called a GET instead of POST -
i have 'post' method
[httppost] [system.web.mvc.validateantiforgerytoken] public httpresponsemessage updateprofile(string sappuser) { mobileprofilemodel profilemodel= jsonconvert.deserializeobject<mobileprofilemodel>(sappuser); using (ucapp = new usercontrollerapplication()) { //this code should match bool success = ucapp.updateuserprofile(profilemodel); var response = request.createresponse<bool>(httpstatuscode.created, success); string uri = url.link("defaultapi", new { result = success }); response.headers.location = new uri(uri); return response; } }
and calling ajax 'post'
$.ajax({ url: "http://mydomain.com/api/user/updateprofile", data:json.stringify(profile), type: 'post', contenttype: 'application/json', //datatype: "json", async: false, cache: false, success: function (data) { $.blockui({ message: "success" }); }, error: function (xhr) { alert(xhr.responsetext); }, beforesend: function() { $.blockui({ message: $("#ajaxloader") }); }, complete: function() { $.unblockui(); } });
and im getting error
<error> <message> requested resource not support http method 'get'. </message> </error>
the problem im not calling method , method isnt marked either. not sure issue is.
update these route definitions
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); //specific route public views routes.maproute( "publicview", "publicview/details/{username}", new { controller = "publicview", action = "details", username = urlparameter.optional } ); routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{action}/{id}", defaults: new { id = routeparameter.optional } ); routes.maproute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "home", action = "index", id = urlparameter.optional } ); }
i executing method on same controller , works, post of initial get.
there 2 things here, firstly there reason why have used full path in ajax request , not absolute path ? example purpose ? recommend you absolute path.
next if try in ajax call, change following
url: "http://mydomain.com/api/user/updateprofile",
to
url: "/api/user/updateprofile/" + profile,
or else if want use data:
attribute in ajax call use this
data: {sappuser: profile},
let me know if doesnt help.
Comments
Post a Comment