前台页面:ViewContext.RouteData.Values["Action"].ToString();
ViewContext.RouteData.Values["Controller"].ToString();
ViewContext.RouteData.Values["ID"].ToString();
后台页面:RouteData.GetRequiredString("Action");
RouteData.GetRequiredString("Controller");
RouteData.GetRequiredString("id");
路由规则是有优先级的,上面的优先级高。
routes.MapRoute( name: "test", url: "test1/{action}/{id}", defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "datacenter", url: "data/{action}", defaults: new { controller = "DataCenter", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
当访问../test1页面时会匹配到第一个路由规则,对应的控制器和行为:Test/Index。访问../test1/add时,对应Test/Add
当访问../data页面时会匹配到第二个路由规则,对应的控制器和行为:DataCenter/Index。访问../data/add时,对应DataCenter/Add
当访问../Test或../DataCenter或../时都会匹配到第三个路由规则,第三个路由规则为系统自动生成的。