我想尝试这样的路线:
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,Index操作不起作用,因为它每次都被重定向到Index。
可以同时获得两者吗?
先谢谢你
答案 0 :(得分:5)
尝试在自定义路线中引入约束,否则不会允许找到默认路线。
routes.MapRoute(
"DefaultIndex", // Route name
"portfolio/{id}", // URL with parameters
new { controller="portfolio", action = "Index", id = UrlParameter.Optional }
);
这样您只能映射以"投资组合"开头的网址。在您的路线中,并指定哪个控制器和操作。其他URL的请求由默认路由处理。
答案 1 :(得分: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
);
}
答案 2 :(得分: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
);
}
如果您提及任何控制器和操作,它将转到其他位置,它将选择默认