2010-10-14 84 views
0

我有一個路線路由在asp.net mvc的

routes.MapRoute(
    "BuildingProject", 
    "BuildingProject/{action}/{id}", 
    new { 
    controller = "Home", 
    action = "Index", 
    id = "" 
}); 

我希望它表現得像缺省路由,即對網址與BuildingProject開始像http://localhost:4030/BuildingProject/DeleteAll。 我試圖

routes.MapRoute(
    "BuildingProject", 
    "BuildingProject/{action}/{id}", 
    new { 
    controller = "Home", 
    action = "", 
    id = "" 
}); 

它worked.But上鍵入本地主機:4030/BuildingProject它不重定向到它的指數,但顯示錯誤。
。如何做到這一點。

回答

0

試試這個:

routes.MapRoute("BuildingProject", "BuildingProject/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); 
+0

sorru但它沒有工作。 – 2010-10-14 15:07:11

+0

你想讓所有的網址指向相同的動作嗎?然後你可以嘗試下面的代碼:routes.MapRoute(null,「BuildingProject/{* catchall}」,new {controller =「Home」,action =「Index」}); – 2010-10-14 15:34:57

1

如果你的路線是這樣的:

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     "BuildingProject", 
     "BuildingProject/{action}/{id}", 
     new 
     { 
      controller = "Home", 
      action = "Index", 
      id = UrlParameter.Optional 
     } 
    ); 
} 

然後http://localhost:4030/BuildingProject/DeleteAll將呼籲Home控制器DeleteAll行動,如果你瀏覽到http://localhost:4030/BuildingProject,該Index動作會在同一個控制器上調用。