ASP. NET MVC part6 WebAPI

Source: Internet
Author: User

Two types of Web services
SOAP Style: Based on method, product is WebService
Rest Style: resource-based, product is WEBAPI
Can return data of JSON, XML type
For data increment, delete, change, check, provide relative resource operation, according to the type of request processing, mainly including get (check), Post (Increase), Put (change), delete (delete), these are the HTTP protocol support request mode
Request method: Request based on routing rules

The method of defining crud in Apicontroller, the name can be customized, if the corresponding resource operation, you can use the attribute constraints
The main features include
HttpGet
HttpPost
Httpput
Httpdelete

How to use 1:jquery Ajax
Specifies the requested data type: ContentType: "Application/json; Charset=utf-8 ",//data type
The main properties:
Type: Request method, including get, Post, Put, Delete
URL: Request resource, written according to routing rules
Data: Request for JSON format
ContentType: Type and encoding of request data
DataType: The type of data returned, which can be text, JSON
Success: Successfully processed callback function
Example of a modification request in a note
Note: Asynchronous operations that use JS cannot be accessed across domains

vardata ='{"UserId": "'+ $('#userId'). Val () +'" ," UserName ":"'+ $('#userName'). Val () +'"}'; $.ajax ({type:'PUT',//The request type. Get,post,put,deleteUrl:'api/userinfo/'+ $('#userId'). Val (),//Request AddressData:data,//ParametersContentType:"Application/json; Charset=utf-8",//Data TypeDataType:'text',//Return Data typesuccess:function (msg) {if(eval (msg) = ='1') {initdata (); }                    }                }); 

How to use 2:httpclient object CRUD Operations API

Create and initialize the object: client. BaseAddress=NewUri (URL); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (NewMediatypewithqualityheadervalue ("Application/json") ); Read collection: Httpresponsemessage response=client. Getasync (URL).     Result; varUserList = Response. content.readasasync<ienumerable< Data Type >>(). Result; Read object according to number Httpresponsemessage Response1=client. Getasync (URL).    Result; varUserInfo = Response1. content.readasasync< Data Type >(). Result; Increase: httpresponsemessage response= client. Postasjsonasync ("Api/userinfo", UserInfo).    Result; Use response. Issuccessstatuscode Determines whether response is used successfully. Content.tostring () Gets the return value modification: httpresponsemessage response= client. Putasjsonasync ("api/userinfo/"+Userinfo.userid, UserInfo).    Result; Use response. Issuccessstatuscode Determines whether response is used successfully. Content.tostring () Get return value Delete: httpresponsemessage response= client. Deleteasync ("api/userinfo/"+uid).    Result; Use response. Issuccessstatuscode Determines whether response is used successfully. Content.tostring () Gets the return value

Principle (Pipeline execution process)

Get Controllerfactory, Call the Controller.execute method in the PR method of the Mvchandler to invoke the Actioninvoker.invokeaction method in the Mvchandler PR method based on the context reflection controller object the current controller class -"Execute method for Controllerbase class-" The Executecore method of the controller class calls the Invokeactionresultwithfilteractioninvoker object to invoke an instance of the Controlleractioninvoker class Actionresult.executer Esult calls the View.render method, renders the page to the output stream Viewresult class -"Viewresultbase class

ASP. NET MVC part6 WebAPI

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.