【问题标题】:How to Avoid Global.asax for 'VaryByCustom' caching如何避免 Global.asax 用于“VaryByCustom”缓存
【发布时间】:2012-10-14 12:05:53
【问题描述】:

我正在写一个自定义的HttpModule,我需要使用:

Response.Cache.SetVaryByCustom("role");

基本上,我需要根据用户的成员角色缓存响应。我的所有研究都将我指向了该页面:

http://msdn.microsoft.com/en-us/library/5ecf4420(v=vs.100).aspx

建议在 Global.asax 中覆盖 GetVaryByCustomString

有没有办法可以避免在 global.asax 中这样做?让我们假设我正在构建一个收缩包装的应用程序。

【问题讨论】:

  • 好问题,我不确定。这是从早期就存在的方法之一,并且在设计时并未考虑到这种可扩展性。所以它需要覆盖全局应用程序,这不是很容易插入。也许其他人会有一些想法。
  • 谢谢@DavidEbbo。一个想法 - 有没有办法在 HttpApplication 被实例化时插入管道,然后创建一个覆盖 GetVaryByCustomString 方法的实例?
  • 这似乎很困难,因为只能有一种应用程序类型。例如假设 global.asax 有一些代码。该代码预计将成为 HttpApplication 的一部分。但是不能同时存在该代码和您自己的 HttpApplication。我们需要多重继承:)
  • 自定义OutputCacheProvider 怎么样? github.com/maxtoroq/DiskOutputCache?

标签: asp.net caching outputcache


【解决方案1】:

为什么“shrinkwrapped”应用程序会排除 Global.asax?

试试这个:

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (custom == "role")
            return Roles.GetRolesForUser().Aggregate((a,b) => a = a+b);
    }

【讨论】:

  • 抱歉,我已经知道该怎么做了。我不想在 global.asax 中这样做。
猜你喜欢
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多