Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Disable view caching in overridden VirtualPathProvider

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?

like image 915
Code Silverback Avatar asked Mar 14 '11 20:03

Code Silverback


1 Answers

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);  
        }
    }
like image 163
Code Silverback Avatar answered Oct 06 '22 00:10

Code Silverback