Routing in the Web API (i)--Basic routing

Source: Internet
Author: User

I. Routing concepts in the Web API

The role of the route is explained in a word: The controller,action that handles the request is found through the URI of request and assigns a value to the action's parameters .

Default routes in the Web API:

" API Default "  "api/{controller}/{id}"new {id =  Routeparameter.optional});

Each route (routing rule object) in the route table contains a route template. The default route template for the Web API is "API/{Controller}/{ID}". In this template, "API" is a text path segment, {controller} and {ID} are placeholder variables.

When the Web API framework receives an HTTP request, it attempts to match the URI with a route template in the route table. If there is no route match, the client receives a 404 error.

Second, the routing process

The routing process has three steps:
1. Match to route template by Uri
2. Obtain the corresponding controller
3. Get the corresponding action

(i) match to route template by Uri

The route template looks much like a URI address, but it contains placeholders, below we add a route (routing rule), the name is Myroute, and when there is no controller, the default controller is the home ; The custom parameter ID adds a constraint: The ID must be a number. When we add the following code, RouteTable has one more route object.

CONFIG. Routes.maphttproute (Name:"Defaultapi", Routetemplate:"Api/{controller}/{id}", defaults:New{id =routeparameter.optional}); Config. Routes.maphttproute (Name:"Myroute", Routetemplate:"Api/{controller}/{action}/{id}",//you can give a placeholder a default valueDefaultsNew{controller ="Home"},//You can add a constraint to a placeholder that matches the routing rule only if the ID is a numberConstraintsNew{id =@"\d+" });

When the framework accepts a request, it looks for a URI-matching routing template, and when the routing template is found, it creates a route Dictionary object that contains the value of each placeholder, which is the name of the placeholder, and the value is obtained from the URI or the default value. This Dictionary object is stored in the Ihttproutedata object.
A chestnut: URI for API/PRODUCTS/FINDPRODUCT/1, matching successful myroute routetemplate success, the created routing dictionary contains the

controller:products action:findproduct ID: 1
(ii) Acquisition of Controller

The default method for a controller is simple:
Locate the controller in the routing dictionary, and hold "controller" after the controller's value. If the controller in our routing dictionary is the value of products, then the controller we are looking for is products+ "Controller" =productscontroller.

(iii), get action

When you get the action, the Web API looks at the HTTP method and then finds the action with the name that starts with the HTTP method name. For example, for a GET request, the Web API looks for an operation that begins with "get ...", such as "getcontact" or "getallcontacts". This Convention applies only to the Get,post,put and delete methods. You can use the properties on the controller to enable other HTTP methods. When multiple methods are found to conform to HTTP method, the matching parameter matches the most.

Iii. routing variants (i) through attribute tags

When we use the [HttpGet] [HttpPost] [httpput] [httpdelete] attribute to decorate the action, the Web API receives a request and also looks up the HTTP method, as in [HttpGet ] To modify the action, we will find the Findproduct (ID) of the chestnut below when we access http://xxx:xx/Products as a get. Note: If you do not have a property label and do not start with an HTTP method, then the default is post.
A chestnut:

 Public class productscontroller:apicontroller{[HttpGet]   public Product findproduct (int id) {}}
(ii) matching multiple HTTP method

If we want a method to match in more than one HTTP method, such as when we use GET or POST request access method Findproduct (ID), how to set it? It can be set using Acceptvervs, and when we use GET or POST request api/products/will match to the method findproduct

 Public class productscontroller:apicontroller{[Acceptverbs ("GET""POST " )]   public Product findproduct (ID) {}}

How to make the normal method in the controller, not to be matched to it? Just add [nonaction] to the top of the method to

[Nonaction]  Public string getstr (Product p) {    return  p.name;}

Routing in the Web API (i)--Basic routing

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.