Building rest services with ASP. WEBAPI (ii)--routing

Source: Internet
Author: User

Instead of explicitly specifying the access path to a server function as the traditional RPC service does, rest maps a URL to a service function entry based on a rule called routing. The route of the ASP. NET Webapi is the same as ASP. NET MVC, which supports two routing methods, traditional route mapping and feature routing. The default content of the routing rule WebApiConfig.cs is defined as follows:

PublicStaticClassWebapiconfig
{
PublicStaticvoidRegister (HttpconfigurationConfig
{
Config. Maphttpattributeroutes ();

Config. Routes.maphttproute (
Name"Defaultapi",
Routetemplate: "Api/{controller}/{id}",
Defaults: new {id = routeparameter. Optional}
);
}
}

It registers two routing rules by default, the first line is the attribute route, and the second line is the traditional mapping route. By default, because we do not write attribute routing rules, we map routes according to traditional controller methods.

With regard to routing rules, the MSDN documentation is very detailed, but due to its introduction too detailed, it is not easy to get into the door, here I only take the default routing rules to simple but the introduction of my understanding, its URI format is this "Api/{controller }/{id}", where the ID is optional. Take the example of the previous article,

    1. when we access the Api/products address, the system will first find a controller named ProductsController.
    2. then, depending on the access method lookup function, because this is a get operation, then find the function at the beginning of get, here will find get () and get (int id) two overloaded versions.
    3. Finally, match the specific function according to the parameters, because there is no parameter ID. So it matches to the get () function and returns all the collections.

In addition, there are several commonly used derivative rules:

    1. depending on how you look at the function, you only need to match the header, so when we write the function it is written Get () and the getproduct () is all that's possible. . ,
    2. When looking for a function according to how it is done, it is not case-sensitive, so it is written Get () or G et () it's all possible.

When we use a version with parameters, there are a few points to note:

    1. parameter names are not case-sensitive, we can write IDs or IDs.
    2. parameter names require strict matching, we write ID2 is not possible, this time will match to the wrong result get ()

The default rules, although most of the time, are more convenient, but many times we need to manually specify a personalized routing rule. For example, we can customize a url:api/products/name=xxx that is queried by name. This time you can use the feature routing to quickly implement:

[Route("Api/{controller}/name={name}")]
publicihttpactionresult getbyname (string name)

About attribute routing, the MSDN original attribute Routing in ASP 5 is introduced in great detail, there are very good translations in the country attribute Routing in ASP 5 translation and supplement, here do not do much introduction The

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.