Mvc-routing-URL Routing

Source: Internet
Author: User

1. Routing: Routing

The main is compared to the HTTP request through the browser to respond to the appropriate URL to the browser.

?
1 @Html.ActionLink("关于","About","Home")

This code generates an HTML hyperlink:

?
1 <a href="/Home/About">关于</a>

  

2. The URL routing rule is defined in the App_start\routeconfig.cs document by default.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 namespace MvcApplication3 {     public class RouteConfig     {         public static void RegisterRoutes(RouteCollection routes)         {             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                 name: "Default",                 url: "{controller}/{action}/{id}",                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }             );         }     } }

1.1 All the intersections of the ASP. NET Web application are in HttpApplication's Application_Start ().

Where Routetable.routes is a public static object. Used to save all URL routing rule collections (routecollection).

in ASP. NET MVC, the program loads the method from the Application_Start () event in Global.asax.

 Routeconfig.registerroutes (routetable.routes); is to load the network routing address

?
1 2 3 4 5 6 7 8 9 10 11 12 public class MvcApplication : System.Web.HttpApplication {     protected void Application_Start()     {         AreaRegistration.RegisterAllAreas();        WebApiConfig.Register(GlobalConfiguration.Configuration);         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);         RouteConfig.RegisterRoutes(RouteTable.Routes);         BundleConfig.RegisterBundles(BundleTable.Bundles);     } }

  

The Ignoreroute method in 1.2 registerroutes is used to define URLs that are not processed via URL routing , and the first parameter of this method is to set the URL style "do not route through URL".

?
1 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

The so-called " do not route URLs through URLs" is: If the client browser sent over the URL in this rule than the success, then the URL will not continue to route through the line.

1.3 The first parameter in the Ignoreroute method, "resource". Represents the RouteValue route variable, which can take any value

{Resource}.axd represents all *.axd document URLs. All. Axd documents usually represent one of the HttpHandler. Just like the. ashx file.

The first parameter in the 1.4 Ignoreroute method also has a {*pathinfo} parameter. is also a routevalue routing variable.

*pathinfo represents the full meaning of crawling.

Complete address "{Resource}.axd/{*pathinfo}" for example:

If the URL is/webresource.axd/a/b/c/d, {Resource}.axd will be compared to WebResource.axd.

and {*pathinfo} will get Ah a/b/c/d, if you remove the * number can only get a.

1.5 MapRoute. is used to define URL routing extensions.

The specified parameter of the Maproute method has been changed to "named parameter".

?
1 2 3 4 5 6 7 private void Me(int x, int y = 6, int z = 7)        {            //....        }   Me(1, 2, 3);// 标准调用   Me(1, 5);//  等同与 Me(1,5,7) ,Z已经定义   Me(1);//也可以

Name: The first route name of the parameter, here is "Default".

URL: named parameter definition. {Controller}/{action}/{id}

The definition URL style contains three route parameters for controller, Action,id

    If you enter a URL/home/about/123, it corresponds to the above three values

The default value of the defaults named parameter, which attempts to bring in the default value defined here when the URL route is less than the HTTP-required URL.

?
1 defaults: new{ controller = "Home", action = "Index", id = UrlParameter.Optional }

  

    

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.