Learning ASP. NET MVC for more than a year, it's time to learn the Web API. The API is similar to the controller of ASP. The former simply returns the data serialized and sent to the client, which returns the view or render view.
When you create the first Web API in a project, it automatically generates a document and modifies web. config. If we first add an API directory under the project, it is used to store the APIs.
After the creation is complete, it displays a txt document:
This readme.txt is to tell you that you need to modify the Globel.asax document, steps to:
In fact, it also automatically produces the following document, WebApiConfig.cs and placed under the App_start directory.
In addition you will find that the Web. config is also modified to automatically add a section:
The following insus.net creates a model:
Create an entity, which is a data center, data from documents, databases, XML, etc.:
Next, we can refine the API, which has inherited the Apicontroller class.
Now let's test if the well-written web API works, and enter it in the address bar of the browser:
404 error, What is the situation? After access to the data, the original Global.asax configuration problems. Because the first API is created, it automatically generates the WebApiConfig.cs and then needs to be manually registered within the Application_Start () method.
But
Routeconfig.registerroutes (routetable.routes);
And
Globalconfiguration.configure (Webapiconfig.register);
is the order of the second relationship. That is, ASP. Globalconfiguration.configure (Webapiconfig.register) before executing routeconfig.registerroutes ( Routetable.routes). We need to be aware of this when we register manually. They are correctly configured as follows:
If you have access, it should show the download status:
When everything is ready, we create an. HTML Web page that uses JavaScript or jquery to read the API's data.
In the project, a new orders directory is used to store everything related to order. html files.
There is no jquery code to write, no problem, I believe it is not difficult, because there have been practice countless times:
Demonstrate:
The practice of ASP. NET MVC Web API