1. Overview
There was a question:where is the difference between ASP. NET MVC and ASP. Webapi? This is actually not a good answer. Perhaps because of the MVC pattern prevalent reason, Webapi seems to be narcissistic, let people think it is chicken ribs. In fact, Webapi should be more in line with the nature of Web application and development than MVC, and the function is not weaker than MVC.
Mvc4 and 5 appear, only webapi appear, as to why it appears? We don't need to study this, we can play it well. The biggest difference between WEBAPI and MVC is that the routing pipeline is inconsistent and each realizes its own set. In the MVC6 era, Microsoft also realized that the code is almost the same, why not reuse it? So now MVC6 has unified the MVC and WEBAPI routes .
When it comes to this, it may seem that MVC and WEBAPI mechanisms and implementations look very much alike. Yes. In fact, the most core of WEBAPI is the use of restful style. about what is rest? We suggest that you understand otherwise. Here's to say, theaction method name in Webapi is either using the HTTP method name or its prefix, or , of course, you can label the http method name attribute on the method without following these. That said, some around, sorry!
2. Create a project
To illustrate, create a givecase.webapiesof an ASP. NET 5 project Template:
Note: The values controller example is already available .
3.Fiddler
This tool is useful for simulating HTTP request debugging and is recommended for use. official website Http://www.telerik.com/fiddler
4. Controller
Open the controllers\ValuesController.cs and modify it to:
usingSystem.Collections.Generic;usingMICROSOFT.ASPNET.MVC;namespacegivecase.webapies.controllers{[Route ("Api/[controller]")] Public classValuescontroller:controller {//Analog Data Staticlist<string> data =initlist (); Private Staticlist<string>initlist () {varList =Newlist<string> {"value1","value2","Value3" }; returnlist; } //get:api/values[HttpGet] Publicienumerable<string>Get () {returndata; } //GET API/VALUES/5[HttpGet ("{ID}")] Public stringGet (intID) {returnData[id]; } //POST api/values[HttpPost] Public voidPost ([Frombody]stringvalue) {data. ADD (value); } //PUT API/VALUES/5[Httpput ("{ID}")] Public voidPut (intID, [frombody]stringvalue) {Data[id]=value; } //DELETE API/VALUES/5[Httpdelete ("{ID}")] Public voidDelete (intID) {data. RemoveAt (ID); } }}
4. Test Demo
Run the Web site using the Fiddler tool.
The 1.get method queries all
Execute execution Result:
The 2.get method queries a single
Note: The index is starting at 0, so the result of a query ID of 1 is the second element of the collection.
3.post method adds a
In the request header plus:
Accept:application/xml
Content-type:application/json
After successful execution ( Note that the status code is 204, should actually be 201, follow-up article re-correction ):
Then use the Get method mentioned above to query all:
Ok! Add success!
4.put method modifies a
We show the change of value1 to Value5
After execution, use the Get method to query all:
OK, also modified success!
5.delete method Deletes a
We're demonstrating the deletion of the 4th Value4.
After execution, use the Get method to query all:
OK, the value is VALUE4 No!
5. Summary
The content of this chapter is not too much, just manipulating the string collection, and not outputting it to an HTML page, or demonstrating the use of a collection of entities (which is the actual application must be ). Knowledge of the HTTP status code is not introduced. The code is not well written, such as error or exception handling where there is no access to the index beyond the bounds.
We learn knowledge, slowly come! Let's Go!
No. 04. ASP. 5:webapi and Fiddler Tools