jquery - MVC Api Controller confusion with JSON.stringify -


i know how post umbraco's api controller, that's not issue.

question:

let have (2) api methods:

    [httppost]     public string test1(string time, string activitytype)     {         return time;     }      [httppost]     public string test2(somemodel model)     {         return model.someproperty;     } 

on first method/ajax call, if stringify "time" , "activitytype", error:

url: '/umbraco/api/someapi/test1', type: 'post', datatype: 'json', data: json.stringify({time: '10', activitytype: 'test'}), 

umbracoapicontroller - no http resource found matches request uri


instead, have append (2) parameters querystring, , works. however, in 2nd api method, have model, , can use stringify method json, , works.

why? same regular mvc well??


we have (2) ajax calls, , these both work:

// can see have append via querystring in instance $.ajax({         url: '/umbraco/api/someapi/test1?time=' + time + '&activitytype=' + activitytype,         type: 'post',         datatype: 'json',         data: '',         // doesn't work ****         // url: '/umbraco/api/someapi/test1',         // data: json.stringify({time: '10', activitytype: 'test'}),         // **********************         processdata: false,         async: false,         contenttype: 'application/json; charset=utf-8',         complete: function (data) {             var test= $.parsejson(data.responsetext);             console.log(test);         },         error: function (response) {             console.log(response.responsetext);         }     });  var post = {    someproperty : 'test',    anotherproperty: 'blahblah' };  $.ajax({         url: '/umbraco/api/someapi/test2',         type: 'post',         datatype: 'json',         data: json.stringify(post),         processdata: false,         async: false,         contenttype: 'application/json; charset=utf-8',         complete: function (data) {             var test= $.parsejson(data.responsetext);             console.log(test);         },         error: function (response) {             console.log(response.responsetext);         }     }); 

webapi's model binder looks in query string default when binding "simple" types strings. different mvc. use [frombody] attribute tell it should in request body instead.

public string test1([frombody]string time, [frombody]string activitytype) 

edit: turns out webapi reads body stream instead of caching in mvc. means [frombody] can applied 1 parameter. need either pass 1 in uri , 1 in body or create complex type contains both of parameters.


Comments