2012-02-29 90 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 
     ); 


    } 

如果您提及任何控制器和动作会去那个否则它会选择默认

相关问题