ASP.NET MVC - menus for different roles

I am writing my first MVC 3 application (in ASP.NET), and I do not know how I can (should) display menus for different users.

My application was created as an MVC3 web application, and the menu looks like this:

<div id="menucontainer"> <ul id="menu"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("Info", "Info", "Home")</li> </ul> </div> 

I created two types of roles: user and admin. Now I want to show other links for users (projects, profile) and for the administrator ("Project Management", "Account Management", "News Management").

How am I supposed to do this?

+8
asp.net-mvc-3
source share
1 answer

I found a solution:

 <div id="menucontainer"> <ul id="menu"> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("Info", "Info", "Home")</li> @if ( Request.IsAuthenticated && HttpContext.Current.User.IsInRole ( "user" ) ) { <li>Projects link</li> <li>Profile link</li> } @if ( Request.IsAuthenticated && HttpContext.Current.User.IsInRole ( "admin" ) ) { <li>Manage Projects link</li> <li>Manage Accounts link</li> } </ul> </div> 
+7
source share

All Articles