• Handler合并js、css文件并缓存在服务器


    <%@ WebHandler Language="C#" class="mergeHandler" %>

    using System; using System.Web; using System.IO; using System.Text;

    public class mergeHandler : IHttpHandler {

        private const string CacheKeyFormt = "CacheKey_{0}_{1}_";     public void ProcessRequest(HttpContext context)     {         MergeFile(context);     }

        public bool IsReusable     {         get         {             return false;         }     }     public void MergeFile(HttpContext context)     {         HttpRequest request = context.Request;         HttpResponse response = context.Response;

            string cachekey = string.Empty;         string content = string.Empty;

            string type = request.QueryString["type"] ?? "";         string setName = request.QueryString["setName"] ?? "";         string fileName = request.QueryString["fileName"] ?? "";

            cachekey = string.Format(CacheKeyFormt, type, setName);

            //HttpRuntime.Cache.Remove(cachekey);

            if (type != "" && setName != "")         {             string path = string.Empty;             if (type.Equals("js"))             {                 response.ContentType = "text/javascript";                 path = context.Server.MapPath("/js/");             }             else if (type.Equals("css"))             {                 response.ContentType = "text/css";                 path = context.Server.MapPath("/css/");             }             if (HttpRuntime.Cache[cachekey] != null)             {                 content = HttpRuntime.Cache[cachekey].ToString();             }             else             {                 StringBuilder sb = new StringBuilder();                 string[] files = Directory.GetFiles(path, "*." + type);

                    if (fileName.IndexOf(",") > -1)                 {                     string[] array = fileName.Split(',');                     for (int i = 0; i < array.Length; i++)                     {                         foreach (string fname in files)                         {                             if (File.Exists(fname) && fname.IndexOf(array[i] + "." + type) > -1)                             {                                 string readstr = File.ReadAllText(fname, Encoding.UTF8);                                 sb.Append(readstr);                             }                         }                     }                 }                 else                 {                     foreach (string fname in files)                     {                         if (File.Exists(fname) && fname.IndexOf(fileName + "." + type) > -1)                         {                             string readstr = File.ReadAllText(fname, Encoding.UTF8);                             sb.Append(readstr);                         }                     }                 }                 content = sb.ToString();                 HttpRuntime.Cache.Insert(cachekey, content, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration);             }         }         response.Write(content);     } }

  • 相关阅读:
    hutool 解析 Excel
    上传文件
    Cannot construct instance of `com.**` (although at least one Creator exists)
    Java8之Optional
    java8之Stream
    java8之Lambda
    springboot+mybatis事务管理
    queryWrapper in like
    Java 组装 Tree
    JWT
  • 原文地址:https://www.cnblogs.com/ajun/p/3062692.html
Copyright © 2020-2023  润新知