1. We recommend async task<> with Async interface
Public Async Task<ihttpactionresult> Get ()
If the return is IEnumerable, use:
return ok<ienumerable<listsitesdetails>> (sites);
If the return is an entity, use:
return OK (entity);
This return is often used when getting a type value:
Public Async Task<ihttpactionresult> Get (int ID)
If you only return a successful result 200 is direct OK (); all right.
If you want to return an entity class and want to return an address route, use:
return Createdatroute ("defaultapi"new {controller="Home"
Go to Api/home/id and return the new content that you just added sites
return Created ("http://www.*.com", sites);
Return to sites and location
2, in the Webapi How to customize the method? How do I invoke a method?
Public Async Task<ihttpactionresult> gettest (string account ) { stringawait Userlogin (" 15 Li Yuan Secondary school ""0000") ; return Ok (restmsg);}
If it is a get access, all methods are preceded by a get; Post is added post, but when called, remove Get/post
such as the above code to call it to do this:/api/users/test?account=james
Of course you can also add routes to change access methods:
[Route ("api/users/gettest/{account}")] Public Async Task<ihttpactionresult> gettest (string account ) { stringawait Userlogin (" 15 Li Yuan Secondary school ""0000") ; return Ok (restmsg);}
The above to invoke is used:/api/users/test/james mode
-------------------------------------------------------------------------------------
Look again at the post example
Public Async Task<ihttpactionresult> postlogin (string School)
This representation is a post access call, which is called:/api/users/login?school= 15 miles Middle School
If you have more than one parameter, it is recommended to use type pass-through instead of string. Like what:
Public Async Task<ihttpactionresult> postlogin (Item)
Public class item{ publicstring school{get; Set;} Public string account{get; Set;} Public string password{get; Set;}}
Call:
var item = {' School ': ' 15 Li Yuan Middle school ', ' account ': ' James ', ' Password ': ' 0000 '}; return $http ({ ' api/user/login ', ' POST ' , ' Application/json ', data: Item}). Then (function (user) { // Check the user was null or not and take AC tion}). Error (function (error) { alert (' invalid ');});
Summary of WEBAPI2 knowledge points