ASP .Net自定义路由无效。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);
//custom route
routes.MapRoute(
"Admin",
"Admin/{addressID}",// controller name with parameter value only(exclude parameter name)
new { controller = "Admin", action = "address" }
new { addressID = @"\d+" }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
public ActionResult address(int addressID = 0)
{
//code and redirection
}
这里我希望隐藏网址中的所有内容(如果可能的话)...就像我想隐藏动作名称和参数名称和值(如果可能)... 建议我这样做的可行方法
就像我想要这样的URL(优先级)
1.http:// localhost:abcd / Admin
要么
2.http:// localhost:abcd / Admin / address
要么
3.http:// localhost:abcd / Admin / 1
要么
4.http:// localhost:abcd / Admin / address / 1
答案 0 :(得分:1)
供快速参考。
routes.MapRoute(
null, // Route name...
address(int? addressID)
并且在你的自定义路线上应该是这样的//custom route
routes.MapRoute(
null, //<<--- set to null
"Admin/{addressID}",// controller name with parameter value only(exclude arameter name)
new { controller = "Admin", action = "address" }
//new { addressID = @"\d+" } <<--- no need for this because based from your example " 2.http: //localhost:abcd/Admin/address" the parameter can be null.
);
感谢