Search code examples
c#cachingvirtualpathprovider

Virtual Path Provider disable caching?


I have a virtual path provider. Problem is its caching my files. Whenever I manually edit one of the aspx files it references the VPP doesn't pull in the new file, it continues to reuse the old file until I restart the site.

I've even over-rode the GetCacheDependency() in my VirtualPathProvider class:

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
    }

Ideas?


Solution

  • Returning a null is essentially telling ASP.NET that you do not have any dependency - hence ASP.NET will not reload the item.

    What you need is to return a valid dependency e.g.

     public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            return new CacheDependency(getPhysicalFileName(virtualPath));
        }
    

    A more correct approach is to make sure that you only handle your own cache dependencies (this is a schematic example) :

     public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            if (isMyVirtualPath(virtualPath))
                return new CacheDependency(getPhysicalFileName(virtualPath));
            else
                return new Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }