ASP.NET MVC 在重写的 VirtualPathProvider 中禁用视图缓存

发布于 2024-10-22 01:57:28 字数 250 浏览 2 评论 0原文

我正在使用可移植区域进行一些开发工作,因此我有一个被覆盖的VirtualPathProvider

我的 public override bool FileExists(string virtualPath) 似乎每隔几分钟就会被调用一次,这意味着 MVC 正在缓存视图。

这在生产中可能很棒,但我不知道如何在开发中关闭它。我希望每次使用视图时都会调用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2024-10-29 01:57:28

为了后代回答我自己的问题......

我们最终覆盖了 GetCacheDependency 调用以确保视图永远不会被缓存。 (我们手动缓存视图)。我们必须创建一个 FakeCacheDependency 来让我们使用缓存中的最后修改日期。

在我们的应用程序中,我们的虚拟视图称为 CondorVirtualFiles。 (构建视图引擎时,您需要给它起一个很酷的名字。)

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);  
        }
    }

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);  
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文