ASP.NET MVC Disable view caching in overridden VirtualPathProvider
Asked Answered
T

1

6

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?

Trisyllable answered 14/3, 2011 at 20:0 Comment(2)
Are you sure it's MVC that's caching the view and not your browser? ctrl-F5 to see if your view gets called.Godrich
Thanks for the question and the answer, it solved my rather annoying caching problem when having a view inside a DLL!Socialism
T
6

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);  
        }
    }
Trisyllable answered 27/5, 2011 at 15:39 Comment(1)
+1 Thank you!!! Just wanted to share some ideas. You have to override the GetFileHash() method as well. Also you could return null instead of a FakeCacheDependency instance. And I found a helpful resource: blog.rocketbase.co.uk/2011/04/asp-net-mvc-virtual-path-providerDitchwater

© 2022 - 2024 — McMap. All rights reserved.