In the MVC logic Code, the Controller and action are required, but in the URL, there is no need to fully embody the 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 routing rules that are registered by default 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 parameters
new { controller =
"Home"
, action =
"Index"
, id = UrlParameter.Optional }
// Parameter defaults
);
}
|
We can define our own routing rules.
Define short URLs
We define a routing rule that can be equivalent to http://localhost/Home/About with Http://localhost/About:
?
routes.MapRoute( "ShortAbout" , "About" , new { controller = "Home" , action= "About" } ); |
Access Http://localhost/About and Http://localhost/Home/About are the same at this time.
The above statement defines only a short URL, for the sake of universality, you can define a routing rule as follows:
?
routes.MapRoute( "ActionOnly" , "{action}/{id}" , new { controller = "Home" , action = "About" , id = UrlParameter.Optional } ); |
To reduce the effect on the default action (URL no action) under other controllers, you can limit the action:
?
routes. MapRoute (      " Actiononly " "{Action}/{id}"      new {controller = "Home" "about" new {action = " about| Index " } ); |
"Go" ASP. NET MVC definition short URLs