Wir haben eine MVC-Anwendung mit verschiedenen Bereichen. Momentan ist nur ein Bereich registriert (in der Konfiguration definiert) und es erfolgt keine Routenregistrierung in der Bereichsregistrierung. Ich habe Probleme mit dem Routing erfahren, da die Url.Action-Methode nicht mehr funktioniert. Meine vereinfachte RouteConfig sieht wie folgt aus:MVC5 Url.Action gibt Null innerhalb des Bereichs zurück
routes.MapRoute(
name: "Second",
url: "second/{action}/{id}",
defaults: new { controller = "Second", action = "Action", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication1.Areas.MyArea.Controllers" }
).DataTokens["area"] = "MyArea";
routes.MapRoute(
name: "Home",
url: "home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication1.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication1.Controllers" }
);
Das Problem, das ist, wenn ich versuche, in der zweiten Controller in MyArea @Url.Action("Index", "Home")
innerhalb Action.cshtml zu nennen es null zurückgibt. Wenn ich das gleiche in Index.cshtml im Home-Controller anrufe funktioniert es gut. Ich habe herausgefunden, dass wenn ich @Url.Action("Index", "Home", new {Area = ""})
in Action.cshtml verwende, es auch gut funktioniert, aber warum das passiert? Wie kann ich das vermeiden, um nur @Url.Action("Index", "Home")
ohne Routenwerte anrufen zu können?