路由到 ASP.NET MVC 中的区域主页

发布于 2024-09-29 12:13:11 字数 481 浏览 2 评论 0原文

我正在尝试路由到 MVC 中某个区域的主页,例如

myDomain.com/myArea/Home/Index

,当我使用 URL 时:

myDomain.com/myArea

MVC 似乎是通过尝试在根控制器文件夹中查找名为“myArea”的控制器,从而路由到:

myDomain.com/myArea/Index

如果控制器存在过。

再次,我想要:

myDomain.com/myArea

路由到:

myDomain.com/myArea/Home/Index

我认为我应该能够在 Global.asax 中执行此操作,而不必创建一个重新路由到区域控制器的虚拟控制器,但我画了一个空白。

I'm trying to route to the home page of an Area in MVC, e.g.

myDomain.com/myArea/Home/Index

when I use the URL:

myDomain.com/myArea

MVC appears to by trying to find a Controller called "myArea" in the root Controllers folder and thereby would route to:

myDomain.com/myArea/Index

if the Controller existed.

Again, I want:

myDomain.com/myArea

to route to:

myDomain.com/myArea/Home/Index

I figure that I should be able to do this in Global.asax without having to resort to creating a dummy controller that re-routes to the area controller, but I've drawn a blank.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋风の叶未落 2024-10-06 12:13:11

我用来解决这个问题的一种方法是在 Global RegisterRoutes 方法中的默认路由上设置名称空间属性,这似乎可以解决该问题。必须将此限制添加到我在主网站和该区域的控制器之间发生冲突的其他一些路线。

var namespaces = new[] { "CompiledExperience.Website.Web.Controllers" };

routes.MapRoute("Default", "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces
);

One way I used to get around this was to set the namespaces property on the default route in the Global RegisterRoutes method, this seemed to resolve that problem. Had to add this restriction to some other routes where I had conflicts between Controller in the main website and the area.

var namespaces = new[] { "CompiledExperience.Website.Web.Controllers" };

routes.MapRoute("Default", "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文