I have an issue with the URL's generated by @Url.Action in views. I am trying to have multiple controllers with the same name in 1 MVC application (modulair application, each module has a prefix and a Home controller). I am using routes with namespaces to get the routing done and that works fine. The URL's generated by Url.Action in the views have an issue where the prefix is not correct. This is my route definition:
//Default added for url building with section, but makes no difference
routes.MapRoute(
name: "Default",
url: "{section}/{controller}/{action}/{id}"
);
routes.MapRoute(
name: "Test1",
url: "Test1/{controller}/{action}/{id}",
defaults: new { section = "Test1", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MVCTest.Controllers.Test1" }
);
routes.MapRoute(
name: "Test2",
url: "Test2/{controller}/{action}/{id}",
defaults: new { section = "Test2", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MVCTest.Controllers.Test2" }
);
This works and the right controllers are resolved when browsing /Test1 and /Test2. Now i have a simple view (Index, used for both actions):
Action url: @Url.Action("Action")
But the result is always 'Action url: /Test1/Home/Action' Why? I tried without the default route but is has the same result (i added default route for the {section} part of the url).
If i only use the default route and use different controller names (or a custom controller factory which uses the section to determine a namespace) the generation of the action url is correct. Is there anything i can do to fix the Url generation in my route setup?