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")
To add a new Area, Right Click on application name from solution explorer -> Select Add -> Select New Scaffolded Item -> select MVC Area from middle pane of dialog box -> Enter Name of Area -> Click Ok.
Add MVC Area with Visual StudioIn Solution Explorer, right click the project and select ADD > New Scaffolded Item, then select MVC Area.
ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.
There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With