View页面根据权限显示不同的内容

1、View中根据权限显示相关操作按钮

@if (CoreTools.CheckAction(HttpContext.Current, "Delete"))
        {
            <div class="pull-right margin-right-10">
                <a id="delete" href="javascript:;" class="btn btn-fit-height red-haze">
                    <i class="fa  fa-trash"></i>&nbsp;删除
                </a>
            </div>
        }

2、权限检查函数

  public static bool CheckAction(string UserAccount, string ControllerName, string ActionName, bool SessionCache = true)
        {
            return GetRoleAction(UserAccount, SessionCache).Where(x => x.ControllerName == ControllerName && x.ActionName == ActionName).Count() > 0 ? true : false;
        }

public static bool CheckAction(HttpContext context, string ActionName, bool SessionCache = true)
        {
            if (context.User.Identity.IsAuthenticated)
            {
                string UserAccount = context.User.Identity.Name;
                string ControllerName = context.Request.RequestContext.RouteData.Values["controller"].ToString();

return CheckAction(UserAccount, ControllerName, ActionName, SessionCache);
            }
            else
            {
                return false;
            }
        }

3、根据权限显示相关菜单项

@foreach (var Menu in CoreTools.GetAllMenu().Where(x => x.RootLevel == 0).OrderBy(x=>x.OrderIndex))
            {
                //判断是否有子菜单
                var SubMenus = CoreTools.GetRoleMenu(HttpContext.Current).Where(x => x.ParentGUID == Menu.GUID && x.RootLevel == 1).OrderBy(x => x.OrderIndex);
                <li>
                    @if (SubMenus.Count() > 0)
                    {
                        <a href="javascript:;">
                            <i class="@Menu.IconClass"></i>
                            <span class="title">@Menu.DisplayName</span>
                            <span class="arrow "></span>
                        </a>
                        <ul class="sub-menu">
                            @foreach (var SubMenu in SubMenus)
                            {
                                <li>
                                    <a href="@SubMenu.RequestUrl" id="@SubMenu.GUID">
                                        <i class="fa @SubMenu.IconClass"></i>
                                        @SubMenu.DisplayName
                                    </a>
                                </li>
                            }
                        </ul>
                    }
                </li>
            }

上一篇:#include


下一篇:Php面向对象 – 单例模式