虚拟路径提供程序禁用缓存?

dow*_*one 8 c# virtualpathprovider caching

我有一个虚拟路径提供程序.问题是它缓存我的文件.每当我手动编辑其中一个aspx文件时,它引用VPP不会拉入新文件,它会继续重用旧文件,直到我重新启动站点.

我甚至在我的VirtualPathProvider类中过度使用了GetCacheDependency():

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

想法?

Sam*_*eth 21

返回null实际上告诉ASP.NET您没有任何依赖 - 因此ASP.NET不会重新加载该项.

你需要的是返回一个有效的依赖,例如

 public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return new CacheDependency(getPhysicalFileName(virtualPath));
    }
Run Code Online (Sandbox Code Playgroud)

更正确的方法是确保您只处理自己的缓存依赖项(这是一个示意图):

 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);
    }
Run Code Online (Sandbox Code Playgroud)

  • 正确答案进一步向下/ Chandima Prematillake (2认同)

小智 16

禁用缓存的正确方法是:

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

    public override String GetFileHash(String virtualPath, IEnumerable virtualPathDependencies)
    {
        if (_IsLayoutFile(virtualPath))
        {
            return Guid.NewGuid().ToString();
        }

        return Previous.GetFileHash(virtualPath, virtualPathDependencies);
    }
Run Code Online (Sandbox Code Playgroud)