I do not have in-depth research on WEBAPI, the initial contact found in the data request a few phenomena.
First cut into the code
1. If the action starts with GET, the default is Get mode, without get the default is the Post method
Public stringGetUsers0 (stringID) {List<UserProfile> list = Userprofilebiz.getbatchtest () asList<userprofile>; //returns a JSON string returnjsonconvert.serializeobject (list); } $ (function () {$.ajax ({URL:"Http://localhost:6556/api/User/GetUsers0", type:"Get", DataType:"JSON", data: {"ID":"1"}, Beforesend:function (XMLHttpRequest) {}, Success:function (data, status) { $ (data). each (function () {alert ( This. UserName); }); }, Complete:function (data, status) {}, Error:function () {}}); });
The Get method can request to the value, the ID case problem can be ignored, but if changed to post, of course, the request is not
If the request is changed to post
will have to
[HttpPost] Public string GetUsers0 ([frombody]string id) { Listas list<userprofile>; // returns a JSON string return jsonconvert.serializeobject (list); }
Now say action to any without get start, and then use get to request Discovery request, this is because the action by default has become the way of POST request, after changing to post, no problem
Public stringMyUsers0 ([Frombody]stringID) {List<UserProfile> list = Userprofilebiz.getbatchtest () asList<userprofile>; //returns a JSON string returnjsonconvert.serializeobject (list); } $.ajax ({URL:"Http://localhost:6556/api/User/MyUsers0", type:"Post", DataType:"JSON", data: {"ID":"1"}, Beforesend:function (XMLHttpRequest) {}, Success:function (data, status) { $ (data). each (function () {alert ( This. UserName); }); }, Complete:function (data, status) {}, Error:function () {}});
But still need a frombody attribute declaration, for non-object type of the value of the way
What happens if I change to an entity parameter?
Public string MyUsers0 (userprofile user) { Listas list<userprofile>; // returns a JSON string return jsonconvert.serializeobject (list); }
You do not need to declare the Frombody attribute for an entity type parameter.
As to why we need to continue to explore.
An initial exploration of ASP Webapi (i.)