日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET 打包多CSS或JS文件以加快页面加载速度的Handler

發布時間:2024/6/14 asp.net 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET 打包多CSS或JS文件以加快页面加载速度的Handler 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ASP.NET 打包多CSS或JS文件以加快頁面加載速度的Handler,

使用<link type="text/css" rel="Stylesheet" href="HttpCombiner.ashx?" />,具體的參數請參考程序中的介紹。附件

using System; using System.IO; using System.IO.Compression; using System.Net; using System.Text; using System.Web; using System.Web.Services;namespace SLTech.DST.Web.Application {/// <summary>/// Summary description for $codebehindclassname$/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class HttpCombiner : IHttpHandler{private const bool DO_GZIP = true;private readonly static TimeSpan CACHE_DURATION = TimeSpan.FromDays(30);private const string DEFAULT_CSS = "decision-support-toolkit.css,controls/tab.css,controls/extension-button.css,";public void ProcessRequest(HttpContext context){var request = context.Request;// Read setName, contentType and version. //All are required. They are used as cache keyvar setName = request["s"] ?? string.Empty;var contentType = request["t"] ?? string.Empty;var version = request["v"] ?? string.Empty;var files = request["f"] ?? string.Empty;files = DEFAULT_CSS + files;// Decide if browser supports compressed responsevar isCompressed = DO_GZIP && this.CanGZip(context.Request);// Response is written as UTF8 encoding. var encoding = new UTF8Encoding(false);// If the set has already been cached, write the response directly from// cache. Otherwise generate the response and cache itif (!this.WriteFromCache(context, setName, version, isCompressed, contentType)){using (var memoryStream = new MemoryStream(5000)){// Decide regular stream or GZipStream based on whether the response// can be cached or notusing (var writer = isCompressed ?(Stream)(new GZipStream(memoryStream, CompressionMode.Compress)) :memoryStream){// Load the files defined and process each filevar fileNames = files.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries);foreach (string fileName in fileNames){var fileBytes = this.GetFileBytes(context,"css/"+fileName.Trim(), encoding);writer.Write(fileBytes, 0, fileBytes.Length);}writer.Close();}var responseBytes = memoryStream.ToArray();context.Cache.Insert(GetCacheKey(setName, version, isCompressed),responseBytes, null, System.Web.Caching.Cache.NoAbsoluteExpiration,CACHE_DURATION);// Generate the responsethis.WriteBytes(responseBytes, context, isCompressed, contentType);}}}private byte[] GetFileBytes(HttpContext context, string virtualPath, Encoding encoding){if (virtualPath.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase)){using (var client = new WebClient()){return client.DownloadData(virtualPath);}}else{var physicalPath = context.Server.MapPath(virtualPath);var bytes = File.ReadAllBytes(physicalPath);return bytes;}}private bool WriteFromCache(HttpContext context, string setName, string version,bool isCompressed, string contentType){var responseBytes = context.Cache[GetCacheKey(setName, version, isCompressed)] as byte[];if (null == responseBytes || 0 == responseBytes.Length) return false;this.WriteBytes(responseBytes, context, isCompressed, contentType);return true;}private void WriteBytes(byte[] bytes, HttpContext context,bool isCompressed, string contentType){var response = context.Response;response.AppendHeader("Content-Length", bytes.Length.ToString());response.ContentType = contentType;if (isCompressed)response.AppendHeader("Content-Encoding", "gzip");context.Response.Cache.SetCacheability(HttpCacheability.Public);context.Response.Cache.SetExpires(DateTime.Now.Add(CACHE_DURATION));context.Response.Cache.SetMaxAge(CACHE_DURATION);context.Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate");response.OutputStream.Write(bytes, 0, bytes.Length);response.Flush();}private bool CanGZip(HttpRequest request){var acceptEncoding = request.Headers["Accept-Encoding"];return (!string.IsNullOrEmpty(acceptEncoding) &&(acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate")));}private string GetCacheKey(string setName, string version, bool isCompressed){return "HttpCombiner." + setName + "." + version + "." + isCompressed;}public bool IsReusable{get{return false;}}} }

?

轉載于:https://www.cnblogs.com/sntetwt/p/3375586.html

總結

以上是生活随笔為你收集整理的ASP.NET 打包多CSS或JS文件以加快页面加载速度的Handler的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。