举个简单的例子,你可以这样做:
<%
if (User.IsInRole("AdminRole")
Html.RenderPartial("AdminMenu");
else if (User.IsInRole("Approver")
Html.RenderPartial("ApproverMenu");
else if (User.IsInRole("Editor")
Html.RenderPartial("EditorMenu");
%>
或者是你的用户可以在多个角色,在这种情况下是这样的逻辑可能更合适:
<%
if (User.IsInRole("AdminRole")
Html.RenderPartial("AdminMenu");
if (User.IsInRole("Approver")
Html.RenderPartial("ApproverMenu");
if (User.IsInRole("Editor")
Html.RenderPartial("EditorMenu");
%>
或者使用扩展方法为后者提供更优雅的方法:
<%
Html.RenderPartialIfInRole("AdminMenu", "AdminRole");
Html.RenderPartialIfInRole("ApproverMenu", "Approver");
Html.RenderPartialIfInRole("EditorMenu", "Editor");
%>
与
public static void RenderPartialIfInRole
(this HtmlHelper html, string control, string role)
{
if (HttpContext.Current.User.IsInRole(role)
html.RenderPartial(control);
}
是的,我希望的东西多了几分优雅!但我同意这是一个工作。 – Rippo 2009-11-17 15:43:50
感谢您的回答! – Rippo 2009-11-17 15:44:56
@Rippo是的,我明白了。其实,你可以尝试一种扩展方法。我会举一个例子。 – Joseph 2009-11-17 15:45:33