Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Areas with shared layout

I have defined an area (Admin) in my ASP.NET MVC 3 application, created _ViewStart.cshtml in that area and addedLayout = "~/Views/Shared/_Layout.cshtml"; to it to have a unified site layout.

I also added the following code to _Layout.cshtml:

if (HttpContext.Current.User.IsInRole("Admin")) {     <li>@Html.ActionLink("Items List", "Index", "Items", new { area = "Admin" }, null)</li> } 

The Admin area is displayed properly having _Layout.cshtml as its layout. But all the navigation links in the page now point to Admin subfolder.

For example in my layout I have <li>@Html.ActionLink("About Us", "About", "Home")</li>, which points to Mysite/Home/About. But after clicking the admin link, the "About Us" link points to /Admin/Home/About.

What should I do to have _Layout.cshtml links pointing to the right address?
Thanks.

like image 233
Kamyar Avatar asked Oct 19 '11 21:10

Kamyar


People also ask

What are areas in ASP.NET MVC?

Area allows us to partition the large application into smaller units where each unit contains a separate MVC folder structure, same as the default MVC folder structure. For example, a large enterprise application may have different modules like admin, finance, HR, marketing, etc.

Can we use multiple layout in MVC?

Yes, we can use multiple Layout in ASP.Net MVC application. By default, Visual Studio adds a Layout page in a shared folder which can be used by other View pages if required. In some cases, we can have the requirement to use multiple shared layout pages in MVC application.

Can partial view have layout?

Yes, even in Partial View also we can set Layout property which turns Partial View as normal View with Layout .

Can we have multiple _ViewStart in MVC?

We can also create multiple _ViewStart. cshtml pages. The file execution is dependent upon the location of the file within the folder hierarchy and the view being rendered. The MVC Runtime will first execute the code of the _ViewStart.


1 Answers

Simply specify a blank area for them if they are to be served from root controllers:

<li>@Html.ActionLink("About Us", "About", "Home", new { area = "" }, null)</li> 
like image 79
Darin Dimitrov Avatar answered Oct 11 '22 15:10

Darin Dimitrov