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.
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.
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.
Yes, even in Partial View also we can set Layout property which turns Partial View as normal View with Layout .
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.
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>
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