ASP.NET MVC 4你怎么会根据角色不同的HTML? [英] ASP.NET MVC 4 How do you serve different HTML based on Role?

查看:95
本文介绍了ASP.NET MVC 4你怎么会根据角色不同的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在仅允许某些角色访问特定的控制器或者动作的顶部,我想根据角色来服务一个稍微不同的HTML。

On top of allowing only certain Roles to access certain Controller or Action, I would like to serve a slightly different HTML based on the roles.

管理员可以看到编辑按钮,例如,同时为其他用户此编辑按钮将不会存在。它可以比这更复杂,比如一定的作用可以编辑,但不能删除。等等。

Admin can see Edit button for example, meanwhile for other user this edit button will not be there. It can be more complicated than this, for example certain role can edit but can't delete.. etc..

有在4 MVC框架做到这一点?

Is there a framework in MVC 4 to do this?

问题在这里似乎有趣,<一个href=\"http://stackoverflow.com/questions/18874081/asp-net-mvc-alternatively-rendering-editorfor-based-on-user-role\">ASP.NET基于用户角色 MVC或者渲染EditorFor

The question here seems interesting, ASP.NET MVC Alternatively Rendering EditorFor Based on User Role

但我不知道这是否是做的正确方法。

But I am not sure whether this is the proper way to do it.

推荐答案

有关的菜单,可以考虑使用 MVCSiteMapProvider ,其中,像其他SiteMapProviders,可以配置为使用安全修整,即以当前用户被授权使用的只显示节点。即站点地图可以用来产生,它会自动尊重授权属性上的控制器和操作的菜单。

For menus, consider using MVCSiteMapProvider, which, like other SiteMapProviders, can be configured to use "security trimming", i.e. to only display nodes that the current user is authorized to use. I.e. a site map can be used to generate a menu which will automatically respect the Authorize attributes on controllers and actions.

有关的控制可见性(编辑按钮,删除按钮等),布尔属性添加到模型: CanEdit CanDelete 等,并有您的控制器填充它们根据用户的角色信息(User.IsInRole)。

For visibility of controls (edit button, delete button and the like), add boolean properties to your Model: CanEdit, CanDelete etc, and have your controller populate them based on the user's roles (User.IsInRole).

关注点分离规定,你不应该在视图直接访问角色。

Separation of concerns dictates that you shouldn't access Roles directly in the View.

这篇关于ASP.NET MVC 4你怎么会根据角色不同的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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