在mvc3中将outputcache子类化 [英] subclassing outputcache issues in mvc3

查看:62
本文介绍了在mvc3中将outputcache子类化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在了解在MVC3中创建OutputCacheAttribute的简单子类时发生的情况时,我遇到了一些问题.这是代码:

I am having some issues understanding what is happening when I create a simple subclass of OutputCacheAttribute in MVC3. Here is the code:

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中,有用于检查缓存页面并返回结果的代码:

The first time a controller action with this attribute is requested, the constructor and all overridden methods are hit, but if I refresh the page, none of the methods or the constructor are hit. It is as if the cache is being read from outside the OutputCacheAttribute, but looking at the MVC source code for OutputCacheAttribute, I can see that in OnActionExecuting, there is code for checking for a cached page and returning the result:

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

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

Can anyone shed any light on what is happening?

推荐答案

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

It seems as though the OutputCache filter is more complicated than it originally appears. For page caching, it hooks in to the standard ASP.NET output caching mechanism which uses the OutputCacheModule HttpModule in IIS. Once the filter is hit once and adds the page to the cache, subsequent requests do not hit the filter in any way. The OutputCacheModule intercepts these requests and returns the cached object higher up the pipeline.

对于动作缓存,使用单独的机制.这将使用静态MemoryCache,并且构造函数和所有重写的方法都会在每个请求上被击中.

For action caching, a separate mechanism is used. This uses a static MemoryCache and the constructor and all overridden methods are hit on every request.

这篇关于在mvc3中将outputcache子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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