ASP.Net MVC 2 区域、子区域和路由

发布于 2024-09-10 03:11:59 字数 2084 浏览 2 评论 0原文

我一直在寻找解决我的问题的方法。发现了很多类似的问题,但没有一个为我带来解决方案。

我正在尝试在区域内注册区域。这是可行的,但是它“部分”搞乱了我的路由。

我的路线注册按照注册的顺序进行,考虑 FooBar 和 Foo 注册来自 AreaRegistrations

 routes.MapRoute("FooBar_default",
           "Foo/Bar/{controller}/{action}",
           new { area = "Foo/Bar", controller = "Home", action = "Index"},
           new[] { BarHomeControllerType.Namespace }
  );

  routes.MapRoute("Foo_default",
            "Foo/{controller}/{action}/{id}",
            new { area = "Foo", controller = "Start", action = "Index", id = UrlParameter.Optional },
            new { controller = new NotSubArea()},
            new[] { typeof(StartController).Namespace }
        );

   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRoute("PagesRoute", "Pages/{action}", new { controller = "Pages", Action "Index" }).DataTokens["UseNamespaceFallback"] = false;

   routes.MapRoute("Default", // Route name
            "{controller}/{action}/{id}", 
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { typeof(HomeController).Namespace }
            ).DataTokens["UseNamespaceFallback"] = false;

现在出现以下问题。当转到 Website/Foo/ 或 Website/Foo/Bar 时,这些页面中的链接会使用以下命令正确生成:

  !{Html.ActionLink<HomeController>(c => c.Index(),"Home", new { area = "Foo/Bar"})}
  or
  !{ Url.Action("Index", "Home", new { area = "Foo/Bar"}) } //or a different area

但是,当我在主页中使用此链接时,换句话说 Website/ 或 Website/Home 等。

  !{Html.ActionLink<HomeController>(c => c.Index(),"Home", new { area = ""})}
  or
  !{ Url.Action("Index", "Home", new { area = ""}) } 
  //or with no area identifier specified

它会生成 Url: Website /Foo/Bar/Home 等...这当然是错误的。

当我删除 Foo/Bar 的区域注册时,一切都会再次起作用。直接访问 url Website/Home/About 或 Website/Home 确实会显示正确的页面,所以我猜测内部 UrlHelper 以某种方式选择了错误的路由进行渲染。

我尝试切换 FooBar_default 和 Foo_Default 路由的顺序,以便 Foo_default 路由在 FooBar_default 路由之前注册,但随后该区域不再工作(找不到资源)并且链接仍然生成不正确。

我发现最奇怪的是删除 Foo/Bar 注册解决了问题。我希望有人能够对这个问题发表一些见解。

I have been looking around for a solution for my problem. Found alot of similar issues, but none of them led to a solution for me.

I am trying to register an Area within an Area. This works however it "partially" screws up my routing.

My route registrations in the order they are registered, consider the FooBar and Foo registrations to be coming from AreaRegistrations

 routes.MapRoute("FooBar_default",
           "Foo/Bar/{controller}/{action}",
           new { area = "Foo/Bar", controller = "Home", action = "Index"},
           new[] { BarHomeControllerType.Namespace }
  );

  routes.MapRoute("Foo_default",
            "Foo/{controller}/{action}/{id}",
            new { area = "Foo", controller = "Start", action = "Index", id = UrlParameter.Optional },
            new { controller = new NotSubArea()},
            new[] { typeof(StartController).Namespace }
        );

   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRoute("PagesRoute", "Pages/{action}", new { controller = "Pages", Action "Index" }).DataTokens["UseNamespaceFallback"] = false;

   routes.MapRoute("Default", // Route name
            "{controller}/{action}/{id}", 
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { typeof(HomeController).Namespace }
            ).DataTokens["UseNamespaceFallback"] = false;

Now the following problem occurs. When going to Website/Foo/ or Website/Foo/Bar links in those pages are generated correctly using:

  !{Html.ActionLink<HomeController>(c => c.Index(),"Home", new { area = "Foo/Bar"})}
  or
  !{ Url.Action("Index", "Home", new { area = "Foo/Bar"}) } //or a different area

However when i use this in my main pages, in other words Website/ or Website/Home etc..

  !{Html.ActionLink<HomeController>(c => c.Index(),"Home", new { area = ""})}
  or
  !{ Url.Action("Index", "Home", new { area = ""}) } 
  //or with no area identifier specified

It generates the Url: Website/Foo/Bar/Home etc... Which ofcourse is wrong.

When i remove the Area registration for Foo/Bar it all works again. Going to the urls Website/Home/About or Website/Home directly does display the right pages, so im guessing somehow the internal UrlHelper is picking the wrong routes to render.

I tried switching the order of the FooBar_default and Foo_Default routes, so that the Foo_default route is registered before the FooBar_default route, but then the area does not work anymore (resource not found) and the links are still generated incorrectly.

What i find most odd is that removing the Foo/Bar registration solves the problem. I was hoping someone could shed some insight on this matter..

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

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

发布评论

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

评论(1

辞旧 2024-09-17 03:11:59

您需要了解的是,Area 只是一个路由概念,Microsoft 已经巧妙地包装了该概念或 UrlRouting 来帮助人们入门。

实际上,您可以让 MVC 框架根据您的要求来路由您的请求。

您可能需要考虑编写自己的 RouteHandler。这将使您能够根据您的要求正确指导 MVC 框架如何路由任何请求。

请参阅 asp.net 的此答案。以树路径的 net mvc 复杂路由为例来帮助您入门。

chris166 概述了我实现您自己的 IRouteHandler,并映射您的路线以使用它应该可以满足您的需求。它比使用现成的区域解决方案要花费更多的精力,但应该会给您带来更好的结果。

routes.MapRoute(
    "Tree",
    "Tree/{*path}",
    new { controller = "Tree", action = "Index" })
            .RouteHandler = new TreeRouteHandler();

What you need to understand that an Area is just a routing concept which Microsoft have neatly wrapped up the concept or UrlRouting to get people started.

You can actually get the MVC framework to route your request however you like according to your requirements.

What you might need to look at doing, is writing your own RouteHandler. This will enable you to correctly direct how the MVC framework routes any request accoring to your requirements.

See this answer to asp.net mvc complex routing for tree path as an example to get you started.

chris166 outlines that my implementing your own IRouteHandler, and mapping your route to use that instead should get you what you need. Its a bit more effort than using out the box solution of areas, but should get you better results.

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