An initial exploration of ASP. NET MVC Learning Series (i.)-WEBAPI

Source: Internet
Author: User


Since the new project plan to be taken up is being developed with ASP. NET MVC3, I have been reading the relevant book or article for some time recently. Because before in the university has also studied MVC2 development, has done a few simple MVC2 small test project, but after the work mainly still develops the WebForm project, therefore the MVC thing also gradually forgets.

After this period of time of the system learning, really feel MVC3 compared to the previous MVC2 and WebForm, there is a kind of people can't help but the feeling of cool crooked. In particular, the combination of Razor syntax, LINQ expressions, and so on.

In order to get a good look at some of the problems and knowledge points that you encounter during the learning process, we plan to organize the learning process of MVC into a series of articles, and also hope to help those children who are beginners of ASP. Perhaps the style of the article is not as deep as other articles, mainly through examples to express it. Because oneself is also a beginner, the article inevitably has the mistake, also hoped that each expert can teach a lot, everybody study together.

Well, the nonsense is not much to say, get into the chase.

Personally feel that in MVC, routing rules are a more important point. Remember that I have seen Dudu webmaster an article HttpClient + ASP. NET Web API, another alternative to WCF, for the use of "html+ajax+ general handlers" to develop projects, Think this is another way to handle data requests at a higher level, so let's start with this "lite version of Webapi" today.

First, you first create an ASP. NET Empty Web application:

Then add references to "System.Web.Http" and "System.Web.Http.WebHost":

Add a reference to "System.Net.Http" again:

Because of the need to work with Json data, you also add a reference to "Newtonsoft.json".

Here's a little bit to note:

If you start without adding a reference to "Newtonsoft.json", you will get an error when the project runs, and the reason for the error is later.

A reference to four class libraries:

Then create a new class to register the default route map, where the class name is Webapiconfig:

Add the Global.asax file to initialize the route map in the Application_Start method:

        protected void Application_Start (object sender, EventArgs e)        {            //register the route map at application startup            Webapiconfig.register ( globalconfiguration.configuration);        }

Remember to introduce namespaces:

Using System.Web.Http;


As we mentioned above, if you do not reference "Newtonsoft.json" at the beginning, then registering the route map at run time will cause an error:

Create a folder named Controller, then add a class and the class name ends with a controller, here is Usercontroller. Let this class inherit from the Apicontroller base class:

Create a folder named model to hold the entity classes and add the users class:

In the Usercontroller class, add a GetUser () method to simulate some data:

Introducing namespaces using system.web.http;using x_1_firstwebapi.model;namespace x_1_firstwebapi.controller{public    class Usercontroller:apicontroller    {public        list<users> GetUser ()        {            var userlist = new List<users > {             new users{id=1,uname= "Zhang San", uage=12,uaddress= "Haidian"},            new users{id=2,uname= "John Doe", uage=23,uaddress= "Changping District "},            new users{id=3,uname=" Harry ", uage=34,uaddress=" Chaoyang "}            ;            var temp = (from u in userlist                        select u). ToList ();            return temp;}}    }

We previously added a routing rule of "Api/{controller}/{action}/{id}", so the URL of the data we access in the browser is http://localhost:****/api/controllername/ The form of ActionName, here is the Api/user/getuser:

OK, here, the Lite version of the WEBAPI project is complete, which should be for the most part. NET programmer, it is easy to understand.

In the next article, we'll go deep and talk about how to call WEBAPI Request background Data!

Download code

An initial exploration of ASP. NET MVC Learning Series (i.)-WEBAPI

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.