/// <summary> /// 计算响应头ContentType对应的内容 /// </summary> /// <param name="extname"></param> /// <returns></returns> private StaticFileOption GetStaticFileOption(string extname) { StaticFileOption option = (StaticFileOption)s_mineTable[extname]; if (option == null) { option = new StaticFileOption(); option.Cache = 3600 * 24 * 365; // 默认缓存1年 option.Mine = GetMimeType(extname); s_mineTable[extname] = option; } return(option); }
/// <summary> /// 设置响应头 /// </summary> private void SetHeaders() { StaticFileOption option = GetStaticFileOption(_fileinfo.Extension); if (option.Cache > 0) { // 设置缓存响应头 _context.Response.AppendHeader("Cache-Control", "public, max-age=" + option.Cache); _context.Response.AppendHeader("X-StaticFileHandler", option.Cache.ToString()); _context.Response.AppendHeader("ETag", _fileinfo.LastWriteTime.Ticks.ToString()); } else { //参考链接:https://www.coderxing.com/http-cache-control.html _context.Response.AppendHeader("Cache-Control", "no-store, max-age=0"); } // 设置响应内容标头 _context.Response.ContentType = option.Mine; }