MVC - Application root appears twice in url using Url.Content/Url.Action

两盒软妹~` 提交于 2019-12-22 13:10:10

问题


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?


回答1:


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");




回答2:


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"]



来源:https://stackoverflow.com/questions/2217697/mvc-application-root-appears-twice-in-url-using-url-content-url-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!