<%@ 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); } }