10

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

fretje
  • 8,322
  • 2
  • 49
  • 61
h3n
  • 5,142
  • 9
  • 46
  • 76

2 Answers2

14

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.

fretje
  • 8,322
  • 2
  • 49
  • 61
0

I prefer RouteLink method since it will render it exactly.

@Html.RouteLink( 
      "Home Page", 
      "Default", 
      new 
      { 
         Action = "Index", 
         Controller = "Home" 
      } 
)

Give it a shot.

adeel41
  • 3,123
  • 1
  • 29
  • 25