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. 
); 

謝謝