具有默认操作和参数的MVC路由,控制器可以执行多个操作 [英] MVC route with default action and parameter, several actions by controller
问题描述
我正在尝试这样的路线:
I m trying to achive route like that:
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
);
}
效果很好,我可以规划路线/ portfolio / landscape但是我的具有登录,注销,索引操作的帐户控制程序不起作用,因为它每次都被重定向到索引。
It works well I can have my route /portfolio/landscape but my Account controler that have SignIn, SignOut, Index actions doesn't work because it gets redirected to Index each time.
是否可以同时获取两者?
is it possible to get both?
多谢您
推荐答案
尝试在您的自定义路线中引入约束
Try to introduce a constraint in your custom route otherwise it won't allow default route to be found.
routes.MapRoute(
"DefaultIndex", // Route name
"portfolio/{id}", // URL with parameters
new { controller="portfolio", action = "Index", id = UrlParameter.Optional }
);
通过这种方式,您仅映射路线中以 portfolio开头的URL,并指定哪个控制器和操作。对其他URL的请求由默认路由处理。
This way you only map URLs starting with "portfolio" in your route, and specify which controller and action. Requests for other URLs are handled by the default route.
这篇关于具有默认操作和参数的MVC路由,控制器可以执行多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!