【问题标题】:subclassing outputcache issues in mvc3在 mvc3 中子类化 outputcache 问题
【发布时间】:2011-09-23 22:06:27
【问题描述】:

当我在 MVC3 中创建 OutputCacheAttribute 的简单子类时,我在理解发生了什么时遇到了一些问题。代码如下:

public class ExampleOutputCacheAttribute : OutputCacheAttribute
{
    public ExampleOutputCacheAttribute()
    {
       // breakpoint here
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // breakpoint here

        base.OnActionExecuting(filterContext);  
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // breakpoint here

        base.OnActionExecuted(filterContext);

    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        // breakpoint here

        base.OnResultExecuting(filterContext);

    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
         // breakpoint here

        base.OnResultExecuted(filterContext);

    }
}

第一次请求具有此属性的控制器操作时,构造函数和所有重写的方法都会被命中,但如果我刷新页面,则不会命中任何方法或构造函数。就好像缓存是从OutputCacheAttribute外部读取的,但是查看OutputCacheAttribute的MVC源代码,我可以看到在OnActionExecuting中有用于检查缓存页面并返回结果的代码:

filterContext.Result = new ContentResult() { Content = cachedValue };

任何人都可以了解正在发生的事情吗?

【问题讨论】:

    标签: asp.net-mvc-3 outputcache


    【解决方案1】:

    OutputCache 过滤器似乎比最初看起来更复杂。对于页面缓存,它连接到标准 ASP.NET 输出缓存机制,该机制使用 IIS 中的 OutputCacheModule HttpModule。一旦过滤器被命中一次并将页面添加到缓存中,后续请求不会以任何方式命中过滤器。 OutputCacheModule 拦截这些请求并返回管道更高层的缓存对象。

    对于动作缓存,使用了一种单独的机制。这使用了一个静态 MemoryCache,构造函数和所有被覆盖的方法在每个请求上都会被命中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2015-02-14
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多