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

Source: Internet
Author: User






Http://www.cnblogs.com/babycool/p/3861277.html









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 when the application starts
             WebAPIConfig.Register(GlobalConfiguration.Configuration);
         }


Remember to introduce namespaces:


Using System.Web.Http;


Attention:



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:


/ / Introduce the namespace
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="张三", UAge=12, UAddress="海淀区"},
             New Users{Id=2,UName="李四", UAge=23,UAddress="昌平区"},
             New Users{Id=3,UName="王五", UAge=34,UAddress="朝阳区"}
             };

             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

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.