In the logical code of MVC, Controller and action are necessary, but in the Web site, there is no need to fully embody controller and action. For example, we often want to see http://localhost/About rather than http://localhost/Home/About.
Default routing Rules
After you create a new MVC application, the default registered routing rule in Global.asax is:
public static void RegisterRoutes (RouteCollection routes)
{
routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes. Maproute (
"Default",//Route name
"{controller}/{action}/{id}",//URL with parameters
New {controller = "H ome ", action =" Index ", id = urlparameter.optional}//Parameter defaults
);
}
We can define our own routing rules.
Define a short URL
We define a routing rule that can be equated to http://localhost/Home/About with Http://localhost/About:
Routes. Maproute (
"Shortabout",
"about",
new {controller = "Home", action= "about"}
);
Access to Http://localhost/About and Http://localhost/Home/About is the same at this time.
The above statement defines only a short URL, and for the sake of universality, you can define routing rules like this:
Routes. Maproute (
"actiononly",
"{action}/{id}",
new {controller = "Home", action = "about", id = Urlparameter.optional}
);
To reduce the effect of the default action (URL no action) under other controller, you can limit the action:
Routes. Maproute (
"actiononly",
"{action}/{id}",
new {controller = "Home", action = "Index", id = Urlparameter.optional},
new {action = "about| Index "}
);
The above is a small set to introduce the ASP.net MVC definition of short URL method, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone!