ASP. NET MVC Webapi First Contact

Source: Internet
Author: User

The advent of ASP. MVC4 Webapi has been there for a while. Recently because of doing some of their own fun, to do an API, just can learn this webapi.

WEBAPI Project Creation I'll take a look at Webapi's routing configuration.

Config. Routes.maphttproute (                name: "Defaultapi",                routetemplate: "Api/{controller}/{id}",               defaults:new {id = Routeparameter.optional}            );

Direct API and controller controllers name it, which makes people wonder how this configuration can access the controller's method? We all know that the controller can add HttpGet or HttpPost to the method. So we have to add these HttpMethod to the method.

Let's look at the mapping between controller and URL.

Url Controller's action HttpMethod
/api/controllername/id GetUser (int id) HttpGet
/api/controllername/?name=xx GetUser (string name) HttpGet
/api/controllername/ AddUser (Usermodel user) HttpPost
/api/controllername/id DeleteUser (int id) Httpdelete
/api/controllername/ DeleteAll () Httpdelete
/api/controllername/ Putuser () Httpput


Note: getuser (int id) The ID of this method can not be changed, after the change will not be able to access this method. The API configuration like this is simple, but it takes us to each method in the controller with HttpMethod instructions and the method in a controller does not write much. But don't worry, we can define our own routing configuration, as follows

Config. Routes.maphttproute (                name: "Defaultapi",                routetemplate: "Api/{controller}/{action}/{id}",                defaults: New {id = routeparameter.optional}            );

This configuration allows us to use it flexibly.

The above is my personal study of some of the initial shallow understanding, welcome to point out my shortcomings.

ASP. NET MVC Webapi First Contact

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.