简体   繁体   English

在ASP.NET MVC 5中添加具有文件依赖项的操作结果缓存

[英]Add an action result cache with a file dependency in ASP.NET MVC 5

I am using .NET Framework 4.5.1 and ASP.NET MVC 5. 我正在使用.NET Framework 4.5.1和ASP.NET MVC 5。

I have the following action: 我有以下动作:

    [OutputCache(Duration = 86400 /* 24 hours */, VaryByParam = "none")]
    public PartialViewResult WhatsNew()
    {
        var viewModel = new WelcomeWhatsNewViewModel
        {
            WhatsNewHeadlines = _service.GetWhatsNewHeadlines()
        };
        return PartialView("WhatsNew", viewModel);
    }

I am unclear on how I can add an action result cache with a file dependency, so that the cache is cleared when the razor view is updated. 我不清楚如何添加具有文件依赖项的操作结果缓存,以便在更新剃刀视图时清除缓存。 Is this possible? 这可能吗?

I had a look through the ASP.NET MVC 5 source code but I couldn't see how to achieve this, and I did a bit of searching online yet failed to find anything explaining if this can be achieved or not. 我浏览了ASP.NET MVC 5源代码,但看不到如何实现,因此我做了一些在线搜索,但没有找到任何解释此方法能否实现的内容。

When you modify a cshtml file, the next request will force a recompilation and the app domain will need to be unloaded, so httpruntime cache will be cleared which is what backs the outputcache. 当您修改cshtml文件时,下一个请求将强制重新编译,并且需要卸载应用程序域,因此将清除httpruntime缓存,这是支持outputcache的原因。 If for some reason this isn't happening. 如果由于某种原因没有发生。

Here is how you add cache dependency in your action 这是在操作中添加缓存依赖项的方法

Response.AddCacheDependency(new CacheDependency(filename));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM