Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it required to have "area" routevalue on actionlink if the application was grouped into areas?

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

like image 998
h3n Avatar asked Apr 05 '11 09:04

h3n


People also ask

How to add areas in. net Core?

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.

How to add area in MVC visual studio 2022?

Add MVC Area with Visual StudioIn Solution Explorer, right click the project and select ADD > New Scaffolded Item, then select MVC Area.

What is the use of ActionLink in MVC?

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.

What is difference between HTML ActionLink and URL action?

There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


1 Answers

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.

like image 56
fretje Avatar answered Oct 21 '22 03:10

fretje