I have an area called "UserProfile". And from its Index View I want to call an Action from the root controller (Non-Area). I used Html.ActionLink("Index", "Home")
When I ran the application the generated url is "/UserProfile/Home/Index" instead of "/Home/Index".
Root
View Index.aspx
Controller: App/Controller/HomeController
Path: App/Views/Home
Area
View: Index.aspx
Path: App/Areas/UserProfile/Views/User
ActionLink: Html.ActionLink("Index", "Home")
Yes, if you're working with areas you should always specify an Area
in ActionLink
links, an empty one if you don't want the link to go to a specific area, like this:
Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })
This is needed because otherwise, if you don't specify an Area
, the one where the user is in at the moment will be used.
If you for instance use an ActionLink
without specifying an Area
in your _Layout.cshtml
page, it will work as long as you stay in the root of your Application. From the moment you go into an area though, the link will be generated as \currentArea\the_rest_of_the_link
, and hence, won't work anymore.
I prefer RouteLink method since it will render it exactly.
@Html.RouteLink(
"Home Page",
"Default",
new
{
Action = "Index",
Controller = "Home"
}
)
Give it a shot.
来源:https://stackoverflow.com/questions/5549497/is-it-required-to-have-area-routevalue-on-actionlink-if-the-application-was-gr