0

ASP .Net自定义路由不起作用。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如果可能的话...像我想隐藏操作名称和参数名称,如果可能的值... 建议我可能的方式做到这一点

像我想要URL这样的(优先的基础上)
1.http://本地主机:ABCD /管理员
或 2.http://本地主机:ABCD /管理/地址
或 3.http: // localhost:abcd/Admin/1
或 4.http:// localhost:abcd/Admin/address/1

+0

http://stackoverflow.com/a/1479505/1679410 – dakait 2013-03-07 07:21:47

回答

1

供快速参考。

  • 自定义路线应该出现在默认值之前。
  • 尝试将您的自定义路由命名为空。 routes.MapRoute( null, // Route name...
  • 检查您的呼叫是否正确。
  • 如果youre处理那些不接受她在初始负载参数(例如寻呼) makesure你的参数为空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. 
); 

谢谢