I've been trying to come up with a way to create a dynamic role based navigation solution for a project that I am working on.
The navigation should display only links that are relative to the users role, for example: an administrator will have links to view application statistics, manage customer accounts, ect... while a standard user would have links to manage their account, communicate with friends, ect..
I currently have a single partial view called Navigation with some basic conditional statements for role checking and a mix of markup for displaying the appropriate links. This works, but, I know it could quickly become unmanageable.
Navigation Partial View:
@if(User.IsInRole("Admin")) {
<li><a href="#">Statistics</a></li>
<li><a href="#">Accounts</a></li>
<li><a href="#">Dashboard</a></li>
}
@if(User.IsInRole("User")) {
<li><a href="#">Account</a></li>
<li><a href="#">Friends</a></li>
}
// code omitted
Is there a way to get this logic out of the view and let the Controller handle this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…