MVC - 应用程序根目录在使用 Url.Content/Url.Action 的 url 中出现两次

发布于 2024-08-20 17:56:14 字数 1102 浏览 5 评论 0原文

我在同一个域上有几个 mvc 应用程序,每个应用程序都有自己的目录。

mydomain.com/app1
mydomain.com/app2
等等。

当在根级别使用 Url.Content() 和 Url.Action() 时,“app1”部分在 url 中重复两次。

// code used to generate the links
<%= Url.Action("tetris", "Apps") %>
Page Url:               mydomain.com/app1/
rendered link (broken): mydomain.com/app1/app1/Apps.aspx/tetris

application root appears twice in the rendered url when at the root directory
Page Url:               mydomain.com/app1/home.aspx/
rendered link (works):  mydomain.com/app1/Apps.aspx/tetris

application root appears once - everything works as expected 

我的路线 - 我正在使用 Phil haacks 博客文章

routes.MapRoute(
    "Default",
    "{controller}.aspx/{action}/{id}",
    new { action = "Index", id = "" }
);

routes.MapRoute(
    "Root",
    "",
    new { controller = "Home", action = "Index", id = "" }
);

有什么想法吗?

I have several mvc applications on the same domain, each have their own directory.

mydomain.com/app1
mydomain.com/app2
etc..

When using Url.Content() and Url.Action() when at the root level, 'app1' part is repeated twice in the urls.

// code used to generate the links
<%= Url.Action("tetris", "Apps") %>
Page Url:               mydomain.com/app1/
rendered link (broken): mydomain.com/app1/app1/Apps.aspx/tetris

application root appears twice in the rendered url when at the root directory
Page Url:               mydomain.com/app1/home.aspx/
rendered link (works):  mydomain.com/app1/Apps.aspx/tetris

application root appears once - everything works as expected 

my routes - I'm using the routes from Phil haacks blog post

routes.MapRoute(
    "Default",
    "{controller}.aspx/{action}/{id}",
    new { action = "Index", id = "" }
);

routes.MapRoute(
    "Root",
    "",
    new { controller = "Home", action = "Index", id = "" }
);

Any ideas?

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

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

发布评论

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

评论(2

会发光的星星闪亮亮i 2024-08-27 17:56:14

这是因为应用程序按照从最内层目录到最外层目录的顺序处理 web.configs。

您有两种选择来修复此行为。

  1. 指定您希望路由应用于根目录或子目录中的命名空间。 http://msdn.microsoft.com/en-us/library/dd504958。 .aspx

    路线.MapRoute(
    “根”,“”,新的{...},
    new[] { "MyNamespace.For.Root" }
    );

  2. 或者在根目录中将子目录指定为忽略的路由。 http://msdn.microsoft.com/en-us/library/dd505203。 .aspx

    routes.IgnoreRoute("/app1");
    paths.IgnoreRoute("/app2");

That is because applications process web.configs in order of inner most director to outer most directory.

You have two options to fix this behavior.

  1. Specify the namespaces you want the routes to apply to either in the root directory or your sub-directories. http://msdn.microsoft.com/en-us/library/dd504958.aspx

    routes.MapRoute(
    "Root", "", new {...},
    new[] { "MyNamespace.For.Root" }
    );

  2. Or in the root directory specify your sub-directories as ignored routes using. http://msdn.microsoft.com/en-us/library/dd505203.aspx

    routes.IgnoreRoute("/app1");
    routes.IgnoreRoute("/app2");

旧梦荧光笔 2024-08-27 17:56:14

当我遇到这个问题时,这是因为有人在控制器上使用了 RouteArea["app1"] 属性,但也将其包含在操作 GET["apps1/Detail"] 中只是 GET["Detail"]

When I had this issue it was because someone used RouteArea["app1"] attribute on the Controller but also included it on the action GET["apps1/Detail"] instead of just GET["Detail"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文