简体   繁体   中英

Create menu dynamically based on user's role

如何根据当前用户的“角色”动态创建ASP.NET MVC2母版页中的菜单?

The simplest and most straightforward way would be to simply add an if statement in the view markup:

<% if (Page.User.IsInRole("Admin")) { %>
   <%= Html.ActionLink("Admin Tools Index", "Index", "Admin") %>
   <%= Html.ActionLink("Admin Dashboard", "Dashboard", "Admin") %>
<% } %>

Or, you can separate out several items pertaining to a specific role into a partial view:

<% if (Page.User.IsInRole("Admin")) { %>
   <% Html.RenderPartial("AdminMenu"); %>
<% } %>

我不确定MVC,但在“普通”ASP.NET中,可以在运行时选择MasterPage。

If you are using the sitemap file to generate menus then you can probably do it in there. If not, then it depends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM