Translation routing of ASP. NET Web API

Source: Internet
Author: User

Original: Routing in ASP Web API

When we create a new Web API project, a default route is defined in WebApiConfig.cs under the app_start folder:

            CONFIG. Routes.maphttproute (                "defaultapi",                "api/{ Controller}/{id}",                new {id = routeparameter.optional}            );

The "API" is added to the default route to avoid routing conflicts with ASP. Of course, if you don't like the convention, you can modify the default route.

Route matching rule: {controller} and {ID} skipped, only description of action match.

The 1.WEB API first looks for an action named for the HTTP method name that starts with the HTTP method name. Example:

 Public class productscontroller:apicontroller{    publicvoid  getallproducts () {}      Public Ienumerable<product> Getproductbyid (int  ID) {}    public Httpresponsemessage deleteproduct (int  ID) {}}

2. However, the above conventions apply only to get, POST, put, and delete methods. The other HTTP methods can be matched using Acceptverbs attribute, and the previous four methods also apply. Example:

 public  class   productscontroller:apicontroller{[HttpGet]  p Ublic   Product findproduct (ID) {}}  
 public  class   productscontroller:apicontroller{[Acceptverbs (  " get  , "  head   )]    Product findproduct (id) {}  //   WebDAV method  [Acceptverbs ( " Span style= "color: #800000;" >mkcol   )]  public  void   Makecollection () {}} 

3. Match by Action name (rewrite route). Example:

routes. Maphttproute (    "actionapi",    "api/{controller}/{ Action}/{id}",    new {id = routeparameter.optional});

3.1. The GET request according to the above routing rule "api/products/details/1" will match:

 Public class productscontroller:apicontroller{    [httpget]    publicstring Details (int  ID);}

3.2. You can use ActionName attribute to override the action name. Example:

 Public class productscontroller:apicontroller{    [HttpGet]    [ActionName ("Thumbnail"  )]    public httpresponsemessage getthumbnailimage (int  ID);    [HttpPost]    [ActionName ("Thumbnail")]      Public void Addthumbnailimage (int  ID);}

This "api/products/thumbnail/ID" has two matches, corresponding to get and post respectively.

4. If a method does not want to be invoked as an action, you can use Nonaction attribute, for example:

// Not an action method. [nonaction]    Public string GetPrivateData () {...}

Translation routing of ASP. NET 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.