编程语言
首页 > 编程语言> > IIS7 ASP.NET MVC 2资产缓存模块不起作用

IIS7 ASP.NET MVC 2资产缓存模块不起作用

作者:互联网

我有这个HttpModule,用于删除不需要的标头并缓存资产约30天,但它似乎无法正常工作.下面的代码和http响应:

响应:

Cache-Control   private
Content-Type    text/html; charset=utf-8
Content-Encoding    gzip
Vary    Accept-Encoding
Server  Microsoft-IIS/7.5
X-AspNetMvc-Version 2.0
X-AspNet-Version    4.0.30319
X-Powered-By    ASP.NET
Date    Sat, 13 Nov 2010 20:13:57 GMT
Content-Length  1892

码:

public class AssetCacheModule : IHttpModule
    {
        private static readonly List<string> _headersToRemove = new List<string> { "X-AspNet-Version", "X-AspNetMvc-Version", "Etag", "Server", };   
        private static readonly List<string> _longCacheExtensions = new List<string> {".js", ".css", ".png", ".jpg", ".gif",};  

        public void Init(HttpApplication context)
        {
            context.EndRequest += ContextEndRequest;  
        }

        private static void ContextEndRequest(object sender, EventArgs e)
        {
            var context = HttpContext.Current;
            _headersToRemove.ForEach(h => context.Response.Headers.Remove(h));
            var extension = Path.GetExtension(context.Request.Url.AbsolutePath);
            if (_longCacheExtensions.Contains(extension))
            {
                TimeSpan cacheDuration = TimeSpan.FromSeconds(44000);
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetExpires(DateTime.Now.Add(cacheDuration));
                context.Response.Cache.SetMaxAge(cacheDuration);
                context.Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
            }  
        }
        public void Dispose() { }
    }

web.config:

   <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="CuteWebUI.UploadModule" type="CuteWebUI.UploadModule,CuteWebUI.AjaxUploader"/>
      <add name="AssetCacheModule" type="PostHope.Web.UI.AssetCacheModule, PostHope.Web.UI"/>
    </httpModules>

我在想什么???

解决方法:

如果您在IIS7中以集成管道模式运行,则HTTP模块位于以下位置:

<system.webServer>
    <modules>

标签:etag,caching,iis-7,asp-net-mvc-2,c
来源: https://codeday.me/bug/20191105/1996864.html