Summary of WEBAPI2 knowledge points

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.