只是在asp.net mvc的一个动作渲染不同的角色不同的看法 [英] Render different views for different roles just by one action in asp.net mvc

查看:98
本文介绍了只是在asp.net mvc的一个动作渲染不同的角色不同的看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设具有三个部分或所谓的三访问级别的Web应用程序

Suppose a web application which has three part or so-called three access level:


  1. 一个为每一位旅客(只为看到的内容,无需认证)

  2. 一种针对用户(授权用户)

  3. 一个用于管理员(授权的管理员)

现在,管理员在系统和用户可以做一些操作的每个内容和每个操作的访问。我不想为用户创建和管理单独的区域,因为我不想重复同样的code在各个领域。例如两个管理员和用户可以创建产品,请参见产品列表,创建目录和...还每一位参观者还可以看到产品的列表,博客,...
所以它不是分离,使code刚刚分离的任务重复一个好主意。我没有创建任何区域,我想通过定义用户角色来控制身份验证和授权时,他/她是在系统中(的想法!?),但主要的问题是当我想要有独立的用户界面(视图)为用户和管理员。因为我想只使用一个产品,目录,控制器......并设置身份验证和授权他们,我怎么能渲染由管理员和用户的每个请求不同的看法?我也不想让我的code脏通过把一堆的if / else来定义哪些查看呈现(我宁愿来复制code地区!),任何解决方案?

now, administrator has access for every content and every operation in the system and Users could do some operations. I don't wanna to create separate areas for Users and Administrator because I don't want to repeat the same code in every area. for example both admin and user can create product, see the list of products, create catalog and... and also every visitor can also sees the list of product, blog posts, ... So it's not a good idea to separate and make the code duplicated just for separating the tasks. I haven't created any area and I want to control the authentication and authorization by defining the user role when he/she is in the system(ideas!?) but the main issue comes when I want to have separate user interface (views) for users and admin. as I want to use just one Controller for products, Catalog, ... and set authentication and authorization for them, how can I render different view for every request by admin and user? I also don't want to make my code dirty by putting bunch of if/else to define which view to render (I'd rather to duplicate the code in areas!), any solution?

推荐答案

也许最简单的解决办法是写自己的 RazorViewEngine (假设你正在使用剃刀)。

Probably the easiest solution is to write your own RazorViewEngine(Assuming you are using razor).

然后,当你想要检索用户的视图,您可以检查用户角色并分配所需的视图。这是一个基本粗暴例如:

Then when you want to retrieve a view for a user, you can check the user role and assign the view you want. This is a basic and crude example:

public override ViewEngineResult FindPartialView(
  ControllerContext controllerContext, 
  string partialViewName, 
  bool useCache)
{
  if (controllerContext.User.IsInRole("Admin"))
  {
    var adminViewLocations = new string[] {"~/AdminViews/" }
    return new ViewEngineResult(adminViewLocations);
  }
  return base.FindPartialView(controllerContext, partialViewName, useCache);
}

这样做意味着所有用户都使用相同的控制器和验证,但观念的转变基于角色(或任何你想要的)。

Doing this means that all users use the same controllers and authentication, but the views change based on roles (or whatever you want).

您可以阅读更多关于<一个href=\"http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx\"相对=nofollow>自定义视图引擎与动态视图位置。

这篇关于只是在asp.net mvc的一个动作渲染不同的角色不同的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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