ASP. net mvc Web Api practice, asp. netmvcwebapi

Source: Internet
Author: User

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:


 

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.