Routing mechanisms for asp.net mvc: named routes

Source: Internet
Author: User

First look at the difference between a named route and an unnamed one:

Named routes:

Routes. Maproute (
          name: "Test",//Route name
          URL: "Code/p/{action}/{id}",//url with parameters
          defaults:new {cont roller = "section", action = "Index", id = urlparameter.optional}//Parameter defaults
          );

Default route:

Routes. Maproute (
            Default ",//Route name
            " {controller}/{action}/{id} ",//URL with parameters
            New {controller =" Ho Me ", action =" Index ", id = urlparameter.optional}//Parameter defaults

Routing mechanisms in asp.net do not require a route to have a name, and in most cases a route without a name can satisfy most applications. Typically, in order to generate a URL (Uniform Resource Locator), just grab the previously defined route values and hand them over to the routing engine, and the rest has a routing engine to deal with, as we have to introduce, in some cases, using this method can generate two of semantics when choosing a route to generate a URL. But naming a route can solve this problem.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

Below we register two routes:

Routes. Maproute (
            name: "Test",//Route name
            URL: "Code/p/{action}/{id}",//url with parameters
            defaults:new {cont roller = "section", action = "Index", id = urlparameter.optional} 
            );
            Routes. Maproute (
            name: "Default",//Route name
            URL: "{controller}/{action}/{id}",//url with parameters
            defaults : new {controller = "Home", action = "Index", id = urlparameter.optional} 
            );

To generate a hyperlink to each route in the view, we add the following code to the index page below home

@Html. RouteLink ("Test", new{controller= "section", action= "Index", id=123})
@Html. RouteLink ("Default", new{controller= "Home", action= "Index", id=123})

Note here: The above two methods are not sure which route to use to generate URLs, they only provide some route values, as expected, the first method generates a URL to/code/p/index/123, and the second method generates a URL that points to/home/index/123.

For these simple examples above, it's easy to generate URLs, but there are some situations that can still cause headaches.

Suppose we add the following page route to the beginning of the routing list, in order for the/aspx/page.aspx/page to be processed/static/url

Routes. Mappageroute (
        "new", 
        "Static/url", 
        "~/aspx/somepage.aspx"
    );

Note that we can't put this route at the end of the routing list in our experiment, otherwise he can't match the incoming request and we won't see the effect we want. Why is that? Because the default route will match the previous two routes before him, we want to place the route at the beginning of the routing list.

So where does the route above go to the beginning of the routing list? For incoming requests, the route can only match the URL-/static/url request, not any other request, which is exactly what we want. Let's take a look at the URLs we clicked on the two hyperlinks above are not available:

/static/url?controller=section&action=index&id=123

/static/url?controller=home&action=index&id=123

Typically, when a URL is generated using a routing mechanism, the route value we provide is used to populate the URL parameter, but we can see that the route above does not have a URL parameter ("/static/url"), so he can match every URL that might be generated. That is, we have all of the above two links to match this route, so we generate a URL that is not used.

At this point we can specify the route name, not only to avoid ambiguity, but even to some extent improve performance, because the routing engine can directly navigate to the specified route and try to use it to generate URLs.

In the previous example, we generated two links, and we made some changes to see the benefits of a named route (the following code uses named parameters):

@Html. RouteLink (
    linkText: "Route:test",
    routename: "Test",
    routevalues:new{controller= "section", action= "index", id=123})
@Html. RouteLink (
    linkText: "Default",
    routename: "Default",
    routevalues: New {controller= "home", action= "index", id=123})

So we can find the route correctly. " People's names are the initials of their fate. " This sentence also uses the route to generate the URL.

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.