2012-02-29 91 views
0

餘米努力才達到這樣的路線MVC路線:與默認操作和參數,幾個動作由控制器

http://mysite.com/portfolio/landscape

http://mysite.com/portfolio/friends等等

,所以我寫了:

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.MapRoute(
       "DefaultIndex", // Route name 
       "{controller}/{id}", // URL with parameters 
       new { action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 
      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
      ); 

     } 

它運作良好我可以讓我的路線/投資組合/景觀,但我的帳戶控制器有SignIn,SignOut,索引操作不起作用,因爲我t會被重定向到每次索引。

是否有可能得到兩個?

謝謝你提前通過

回答

4

嘗試在你的自定義路由引入約束,否則它不會允許將缺省路由被發現。

 routes.MapRoute(
      "DefaultIndex", // Route name 
      "portfolio/{id}", // URL with parameters 
      new { controller="portfolio", action = "Index", id = UrlParameter.Optional } 
     ); 

這樣,你只圖開始在你的路線「組合」的網址,並指定控制器和動作。其他URL的請求由默認路由處理。

+0

非常感謝你 – 2012-02-29 08:02:19

0

假設有一個很好的理由,現有的路線,這裏是一個方法,使AccountController發揮好這些:

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

     routes.MapRoute(
      "Account", // Account name 
      "account/{action}/{id}", // URL with parameters 
      new { controller = "Account", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

     routes.MapRoute(
      "DefaultIndex", // Route name 
      "{controller}/{id}", // URL with parameters 
      new { action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 
     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

    } 
0

我想你可以反向路線聲明的順序。

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults 
     ); 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     routes.MapRoute(
      "DefaultIndex", // Route name 
      "{controller}/{id}", // URL with parameters 
      new { action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 


    } 

如果您提及任何控制器和動作會去那個否則它會選擇默認

相關問題