Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在IIS7中关闭MVC请求而不是静态文件的缓存?_C#_.net_Asp.net Mvc_Caching_Iis - Fatal编程技术网

C# 如何在IIS7中关闭MVC请求而不是静态文件的缓存?

C# 如何在IIS7中关闭MVC请求而不是静态文件的缓存?,c#,.net,asp.net-mvc,caching,iis,C#,.net,Asp.net Mvc,Caching,Iis,我正在开发一个ASP.NETMVC应用程序。大多数控制器操作都不应该被缓存。因此,我在应用程序\u BeginRequest中没有输出缓存头: protected void Application_BeginRequest() { HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); HttpContext.Current.Response.Cache

我正在开发一个ASP.NETMVC应用程序。大多数控制器操作都不应该被缓存。因此,我在
应用程序\u BeginRequest
中没有输出缓存头:

    protected void Application_BeginRequest()
    {
        HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
        HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoStore();
    }
应用程序正在IIS7上运行,模块配置设置为
runAllManagedModulesForAllRequests=“true”
。这意味着所有静态文件也会通过请求管道(并禁用缓存)


为这些静态文件启用缓存的最佳方法是什么?在
应用程序\u BeginRequest
中设置响应缓存头之前,我是否必须检查扩展名?或者是否有更简单的方法(例如完全绕过静态文件的请求管道)?

您可以有一个缓存过滤器属性,将其应用于所有操作(通过基本控制器或显式地在每个控制器或操作上)。这不会应用于静态文件

可能的[CacheFilter]:

using System;
using System.Web;
using System.Web.Mvc;

    public class CacheFilterAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;

            cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            cache.SetValidUntilExpires(false);
            cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            cache.SetCacheability(HttpCacheability.NoCache);
            cache.SetNoStore();
        }
    }

顺便说一下,您甚至可以从不同的域交付静态文件,就像这样,这将消除您的问题作为副作用。

假设您无法避免使用
runAllManagedModulesForAllRequests=“true”
与Hector的链接一样,您可以检查请求处理程序的类型,只有当请求由MVC处理时,才可以设置缓存头

protected void Application_PreRequestHandlerExecute()
{
    if ( HttpContext.Current.CurrentHandler is MvcHandler )
    {
        HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
        HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoStore();
    }
}

请注意,我已将代码移动到
Application\u PreRequestHandlerExecute
,因为尚未在
BeginRequest
中选择处理程序,因此
HttpContext.Current.CurrentHandler
为空。

这看起来很有效。它还允许您在else语句中为静态文件设置自定义缓存控制.