ASP. net mvc Web Api practice, asp. netmvcwebapi
I have been learning ASP. net mvc for more than a year. Now I want to learn Web APIs. The API is similar to the Controller of ASP. net mvc. The former only returns data serialized and sent to the client; the latter returns View or Render View.
When you create the first Web Api in the project, it automatically generates a document and modifies web. config. For example, we first add an Apis directory under the project to store the api controller.
After the file is created, a txt file is displayed:
This readme.txt tells you to modify the Globel. asax document in steps 1, 2, 3:
In fact, it also automatically generates the following document, WebApiConfig. cs, and put it under the App_Start directory.
In addition, you will find that web. config has been modified, and it automatically adds a section:
The following Insus. NET creates a model:
Create an Entity. The Entity is the data distribution center. The data source is used in documents, databases, xml, and so on:
Next, we can improve the Api, which inherits the ApiController class.
Now, let's test whether the Web Api can run normally. Enter the following in the address bar of the browser:
404 error. What is the situation? After checking the information, the configuration of Global. asax is incorrect. Because it automatically generates WebApiConfig. cs when creating the first api, and then needs to be manually registered in the Application_Start () method.
However
RouteConfig. RegisterRoutes (RouteTable. Routes );
And
GlobalConfiguration. Configure (WebApiConfig. Register );
Is sequential. That is to say, asp.net mvc executes GlobalConfiguration. Configure (WebApiConfig. Register) and then runs RouteConfig. RegisterRoutes (RouteTable. Routes ). We need to note this when registering manually. They are correctly configured as follows:
If the access is normal, it will display the download status:
A. html webpage is created to read api data using javascript or jQuery.
In the case, a new orders directory is used to store a. html file related to orders.
There is still no jQuery code written. There is no problem, I believe it is not very difficult, because there have been countless exercises in the past:
Demo: