MVC 3 - 按角色/功能限制视图的部分和/或控件
我将有许多角色,每个角色都有许多功能,因此我认为 RequireRoles 属性不足以满足我的情况。我需要某种方法来动态地让控制器操作向视图定义视图中的哪些部分和/或控件(而不在视图内添加 if/else 逻辑)。
我的想法是控制器应该告诉视图如何呈现自己,而不是使用 if/else 逻辑告诉视图。
关于如何设计这个有什么想法吗?
I will have many Roles, and each Role has many functions, so the RequireRoles Attribute I don't think will suffice in my case. I need some way to dynamically let the Controller action define to the View what sections and/or controls in the View (without adding if/else logic inside the View).
My thought is that the Controller should be telling the View how to present itself and not the View with the if/else logic.
Any ideas on how to design this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您首先需要创建一个过滤器,您可以使用属性来控制哪些角色看到哪些操作。请参阅 http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs" rel="nofollow"> asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs。
其次,为了解决视图中没有逻辑的问题,您可以对需要角色的每个部分使用子操作。您可以再次将过滤器应用于子操作。有关子操作的更多信息,请参阅:http://msdn.microsoft.com/ en-us/library/ie/ee839451.aspx。
您需要更改的是引发异常的部分。您需要检查正在执行的操作是否是子操作。如果是这样,您需要返回空内容结果。
You need to first of all create a filter which you can use an attribute to control what roles see what actions. See http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs.
Second to solve your problem of no logic in the views you could use child actions for each section which requires a role. Again you could apply your filter to the child actions. For more on child actions see: http://msdn.microsoft.com/en-us/library/ie/ee839451.aspx.
What you would need to change is the section that throws the exception. You'd need to check to see if the action being executed is a child action. If so, you'd want to return an empty content result.