【问题标题】:ASP.NET MVC3, How to Use Different OutputCacheProfile to Different User Role?ASP.NET MVC3,如何将不同的输出缓存配置文件用于不同的用户角色?
【发布时间】:2012-01-03 08:19:08
【问题描述】:

我知道我可以在 web.config 文件中设置 OutputCacheProfiles。

我想知道如何将不同的缓存配置文件应用于页面(控制器)级别的不同的用户角色

【问题讨论】:

    标签: asp.net-mvc-3 web-config browser-cache outputcache user-roles


    【解决方案1】:

    您可以使用 OutputCache 属性装饰控制器,该属性允许将参数作为参数传递。例如;

    [OutputCache(Duration = 3600, VaryByParam = "None")]
    

    您没有理由不能扩展属性以获取更多参数“RoleName”并执行“Roles.IsUserInRole(RoleName)”并根据每个角色加载不同的设置。

    编辑

    在作者的cmets之后,我已经审查了我的解决方案。

    首先,您可以在 Web.config 中定义缓存配置文件;

    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear />
    
          <add name="Default" duration="60" />
          <add name="Admin" duration="10" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
    

    我已经扩展了 OutputCacheAttribute 以说明用户的授权,如果用户进行身份验证,它会加载该 CacheProfile;

    public class AuthorisedOutputCache : OutputCacheAttribute
    {
      public string RoleName { get; set; }
    
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
        // Default Profile.
        CacheProfile = "Default";
    
        if (HttpContext.Current.Request.IsAuthenticated)
        {
          if (Roles.IsUserInRole(RoleName))
          {
            CacheProfile = RoleName;
          }
        }
    
        base.OnActionExecuting(filterContext);
      }
    }
    

    为了完整起见,这里是 Index.cshtml 文件;

    @model DateTime
    
    @{
      ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
      The time is @Model.TimeOfDay.ToString()
    </p>
    

    注意:您必须确保为每个角色定义一个缓存配置文件,以及在未找到角色时定义一个默认值。

    编辑

    作者想知道如何在控制器中设置缓存配置文件,我已经发布了一个可行的解决方案,但我不喜欢它,因为使用了 HttpContext.Items - 那么是否有人可以提出替代方案?

    首先,您必须将 OnActionExecuting 更改为 OnActionExecuted;

    public class AuthorisedOutputCache : OutputCacheAttribute
    {
      public string RoleName { get; set; }
    
      public override void OnActionExecuted(ActionExecutedContext filterContext)
      {
        // Do you wish to force the profile?
        if (HttpContext.Current.Items["Cache.Force"] != null)
        {
          // Force the profile and remove the flag.
          CacheProfile = HttpContext.Current.Items["Cache.Force"].ToString();
          HttpContext.Current.Items.Remove("Cache.Force");
        }
        else
        {
          // If the profile has not been set - use the role based authorisation - 
          // otherwise, carry on as normal.
          if (string.IsNullOrEmpty(CacheProfile))
          {
            CacheProfile = "Default";
    
            if (HttpContext.Current.Request.IsAuthenticated)
            {
              if (Roles.IsUserInRole(RoleName))
              {
                CacheProfile = "Admin";
              }
            }
          }
        }
    
        base.OnActionExecuted(filterContext);
      }
    } 
    

    以下行允许您在控制器内设置配置文件;

    public ActionResult Index()
    {
      // Forces the cache profile to one of the name of "Mandatory".
      HttpContext.Items["Cache.Force"] = "Mandatory";
    
      return View(IndexViewName, DateTime.Now);
    }
    

    如果我能提供进一步的帮助,请告诉我,

    马特

    【讨论】:

    • 谢谢。看起来任何接受临时值的地方都可以用于此方法。视野袋呢?我现在就试试这个方法,不得不说,把方法从 OnActionExecuting 改成 OnActionExecuted 是明智之举。
    • 嗨,马特,经过测试。更改 CacheProfile @OnActionExecuting 有效,但 @OnActionExecuted 无效。 CacheProfile 的值已更改,但缓存结果未更改。也许缓存已经在 OnActionExecuting 之后但在 OnActionExecuted 之前部署。
    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 2011-03-26
    • 2016-06-27
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2019-12-19
    相关资源
    最近更新 更多