开发者

ASP.NET MVC Disable view caching in overridden VirtualPathProvider

开发者 https://www.devze.com 2023-02-16 15:41 出处:网络
I am doing some dev work using portable areas so I have an overridden VirtualPathProvider. Mypublic override bool FileExists(string virtualPath) seems to get called only every few minutes, meaning th

I am doing some dev work using portable areas so I have an overridden VirtualPathProvider.

My public override bool FileExists(string virtualPath) seems to get called only every few minutes, meaning that MVC is caching the views.

This is probably great in production but I can't figure out how to turn it 开发者_高级运维off in dev. I want the VirtualPathProvider to get called on each and every use of the view.

Any suggestions?


Answering my own question for the sake of future generations....

We ended up overriding the GetCacheDependency call to ensure that the view is never cached. (We cache views manually). We had to create a FakeCacheDependency that lets us use the last modified date from our cache.

In our application, our virtual views are called CondorVirtualFiles. (When building a views engine, you need to give it a cool name.)

public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            var view = this.GetFile(virtualPath);
            if (view is CondorVirtualFile)
            {
                FakeCacheDependency fcd = new FakeCacheDependency((view as CondorVirtualFile).LastModified);
                return fcd;
            }
            return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }



 public class FakeCacheDependency : System.Web.Caching.CacheDependency
    {
        public FakeCacheDependency(DateTime lastModified)
        {
            base.SetUtcLastModified(lastModified);
        }
        public FakeCacheDependency()
        {
            base.SetUtcLastModified(DateTime.UtcNow);  
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消