簡単な例として、これを行うことができます:
<%
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);
}