有没有办法根据特定路径进行路由映射
我编写了大量 ASP.NET 代码,但我对 .net MVC 不太熟悉,我注册了一个默认路由,如下所示:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
我想在网站上添加另一个管理员区域,所有 URL 都类似于“http” ://localhost/Administrator/controller1"、"http://localhost/Administrator/controller2"等。我在管理员命名空间中有很多控制器,我试图仅使用一个 MapRoute 注册这些控制器,我这样做了像这样的东西:
routes.MapRoute("Administrator_default", "Administrator/{controller}/{action}/{id}", new { controller = "Administrator", action = "Index", id = "" });
它适用于这些控制器,但一个问题是,在其他一些控制器中,当我尝试执行如下重定向时:
return RedirectToAction("Index", "Forum");
然后我将始终重定向到 http ://localhost/Administrator/Forum 而不是 http://localhost/Forum,这不是一个大问题但让 URL 看起来很奇怪,我试图限制到某些命名空间,但是它不起作用。它看起来就像我试图注册两个默认路由并且 .Net 只是匹配第一个路由,我想知道是否有一种方法可以使其成为两个默认路由并仅映射到特定路径?
I code lots of ASP.NET but I'm kind of new with .net MVC, I've a default route registered like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And I want to add another Administrator area on the site and all the URL would be something like "http://localhost/Administrator/controller1", "http://localhost/Administrator/controller2", etc. I've lot of controllers in the Administrator namespace and I'm trying to register those controller with only one MapRoute, I did something like this:
routes.MapRoute("Administrator_default", "Administrator/{controller}/{action}/{id}", new { controller = "Administrator", action = "Index", id = "" });
it works with those controller but one problem is that in some other controller while I try to do a redirect like:
return RedirectToAction("Index", "Forum");
Then I'll always be redirect to http://localhost/Administrator/Forum instead of http://localhost/Forum, it's not a big issue but make the URL looks strange, I tried to restrict to certain namespace but it's not working. It looks just as I'm trying to register two default route and .Net just match the first one, I'm wondering is there a way to make it two default route and map on only specific path only?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这正是为什么
Areas
添加到 MVC 2 的原因。http://www.asp.net/whitepapers/what-is-new-in-aspnet-mvc#_TOC3_2This exact issue is why
Areas
were added to MVC 2. http://www.asp.net/whitepapers/what-is-new-in-aspnet-mvc#_TOC3_2同意扎克的回答。
并不理想,但您确实可以选择将控制器放在项目的控制器根文件夹(例如/controllers/HomeController.cs)中,以及将控制器放在区域中(可能是显示区域菜单的高级根页面) )。
其次是有关使用 RedirectToAction 方法的快速提示。您也可以使用路由参数指定您想要重定向的区域,例如:
RedirectToAction("Index","Form", new { area = "MyOtherArea" });
Agree with Zach's answer.
Not ideal, but you do have the option to have controllers in the controller root folder (e.g. /controllers/HomeController.cs) of your project as well as the controllers in Areas (maybe high level root pages that display menus for areas).
Secondly a quick tip on using the RedirectToAction method. You can specify the area you would like to redirect too using the route parameters e.g:
RedirectToAction("Index","Form", new { area = "MyOtherArea" });