问题
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.
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" } );
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