The practice of ASP. NET MVC Web API

Source: Internet
Author: User

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

Related Article

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.