如何创建,根据用户的角色有不同的显示视图? [英] How can I create a view that has different displays according to the role the user is in?

查看:151
本文介绍了如何创建,根据用户的角色有不同的显示视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建,根据用户的角色有不同的显示视图。

我应该为不同的角色有不同的看法,或者我应该检查教职员页本身上,而不是在行动中的作用?

我将如何检查视图页上的角色?


解决方案

  

或者我应该使用检查的角色
  教职员页自身,而不是
  动作,如果是这样有人可以PLZ告诉我
  怎么办检查视图页面上


您需要做两件事。检查行动作为一项安全措施角色,并为您的意见启用/禁用特定的控制作用。

在您查看页面检查角色的完整形式为

  HttpContext.Current.User.IsInRole(管理员)

许多开发人员将创建页面辅助方法,您可以与更多的东西简洁为您的应用像

结束

 公共静态布尔IsAdmin(此ViewUserControl PG)
{
    返回pg.Page.User.IsInRole(管理员)
}

那么在你看来,你只需要使用的 this.IsAdmin()

要保持你的看法杂波下考虑使用部分景色

 <若%(IsAdmin())
   {
      Html.RenderPartial(AdminPanel);
   }
   其他
   {
      Html.RenderPartial(UserPanel);
   }
%GT;

I want to create a view that has different displays according to the role the user is in.

Should I create a different view for different roles or should I check the roles on the Veiw page itself rather than in the actions?

How would I check the role on the view page?

解决方案

Or should i use check the roles on the Veiw page its self rather than on actions, if so can someone plz show me how do check that on view page

You need to do both. Check roles on actions as a security measure and check roles on views to enable/disable specific controls.

Within your view page the long form of checking a role is

HttpContext.Current.User.IsInRole("Administrator")

many developers will create page helper methods so you can end up with something more concise for your application like

public static bool IsAdmin(this ViewUserControl pg)
{
    return pg.Page.User.IsInRole("Administrator")
}

then in your view you can just use this.IsAdmin()

To keep your view clutter down look into using partial views

<% if (IsAdmin())
   {
      Html.RenderPartial("AdminPanel");
   }
   else
   {
      Html.RenderPartial("UserPanel");
   }
%>

这篇关于如何创建,根据用户的角色有不同的显示视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆