Asp.net MVC defines the short URL method, asp. netmvc
In the logic code of MVC, the Controller and Action are mandatory, but the Controller and Action do not need to be fully reflected in the website. For example, we often want to see http: // localhost/About instead of http: // localhost/Home/About.
Default routing rules
After the MVC application is created, the default routing rules registered in Global. asax are:
public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute("Default", // Route name"{controller}/{action}/{id}", // URL with parametersnew { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults);}
We can define our own routing rules.
Define short URL
We define a routing rule that can use http: // localhost/About to be equivalent to http: // localhost/Home/About:
routes.MapRoute("ShortAbout","About",new { controller = "Home", action="About" });
Access http: // localhost/About and http: // localhost/Home/About are the same.
The preceding statement defines only one short URL. For the sake of universality, you can define routing rules as follows:
routes.MapRoute("ActionOnly", "{action}/{id}", new { controller = "Home", action = "About", id = UrlParameter.Optional });
To reduce the impact on the default Action under other controllers (the URL has no Action), you can restrict the Action:
routes.MapRoute("ActionOnly", "{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { action = "About|Index" });
The above section describes the Asp.net MVC short URL definition method. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner!