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