using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Text; using System.Text.RegularExpressions; using Microsoft.Ajax.Utilities; using System.Configuration; using System.IO; using System.IO.Compression; namespace mvcDemo.Controllers { public class FileBundlerController : Controller { private static readonly string staticFileRoot = ConfigurationManager.AppSettings["staticFileRoot"]; private static readonly string staticFileCatchRoot = ConfigurationManager.AppSettings["staticFileCatchRoot"]; // // GET: /FileBundler/ // url格式:host/Controller?[type=類型(js、css)href=文件地址(以;分隔)](可以有多個目錄)&t=版本 // demoUrl: http://localhost:4122/FileBundler?type=js&href=js.;admin_common,content_addtop.min&t=2010-10-29_19:16:18 public string Index() { string urlQuery = Request.Url.Query; if (string.IsNullOrWhiteSpace(urlQuery)) { return "參數錯誤!!!"; } string[] paramList = urlQuery.Substring(1).Split('&'); /** * 必須是三個參數type、href、t * **/ if (paramList.Length != 3) { return "參數錯誤!!!"; } Dictionary<string, string> urlParam = new Dictionary<string, string>(); foreach (string item in paramList) { string[] paramItem = item.Split('='); /** * 各參數以=分隔 * **/ if (paramItem.Length != 2) { return "參數錯誤!!!"; } /** * 參數不能重復 * **/ if (urlParam.ContainsKey(paramItem[0])) { return "參數錯誤!!!"; } urlParam.Add(paramItem[0], paramItem[1]); } /** * 三個參數必須是固定的 * **/ if (!urlParam.ContainsKey("type") || !urlParam.ContainsKey("href") || !urlParam.ContainsKey("t")) { return "參數錯誤!!!"; } string fileType = urlParam["type"]; string href = urlParam["href"]; /** * 不參包含/或\(文件名里不能有/或\) * **/ if (href.Contains("/") || href.Contains(@"\")) { return "參數錯誤!!!"; } string fileName = string.Format("{0}{1}.{2}", staticFileCatchRoot, href, fileType); /** * 類型必須為js或css * **/ if (fileType.Equals("js")) { Response.ContentType = "text/javascript"; } else if (fileType.Equals("css")) { Response.ContentType = "text/css"; } else { return "參數錯誤!!!"; } DateTime versionTime = DateTime.MinValue; try { versionTime = DateTime.Parse(urlParam["t"].Replace("_", " ")); } catch{} /** * 時間轉換出錯會執行這里 * **/ if(versionTime == DateTime.MinValue) { return "參數錯誤!!!"; } System.IO.FileInfo fileInfo = null; if (System.IO.File.Exists(fileName)) { fileInfo = new FileInfo(fileName); if (fileInfo.CreationTime == versionTime) { return System.IO.File.ReadAllText(fileName); } } /** * 多個目錄以;分隔 * **/ string[] hrefParam = href.Split(';'); int hrefParamLen = hrefParam.Length; /** * 目錄和文件是以;分隔,所以目錄+文件的個數是2的倍數 * **/ if (hrefParamLen % 2 != 0) { return "參數錯誤!!!"; } StringBuilder content = new StringBuilder(); for (int i = 0; i < hrefParamLen; i++) { string folder = hrefParam[i].Replace(".","/"); folder = string.Format("{0}{1}", staticFileRoot, folder); string[] files = hrefParam[++i].Split(','); foreach (string item in files) { string _fileName = string.Format("{0}{1}.{2}", folder, item, fileType); /** * 文件不存在,輸出錯誤信息 * **/ if (!System.IO.File.Exists(_fileName)) { return "文件未找到!!!"; } content.Append(System.IO.File.ReadAllText(_fileName)); } } string fileContent = Minify(content.ToString(), string.Format(".{0}",fileType)); System.IO.File.WriteAllText(fileName, fileContent); fileInfo = new FileInfo(fileName); fileInfo.CreationTime = versionTime; return fileContent; } private static string Minify(string content, string extension) { Minifier cruncher = new Minifier(); if (extension == ".js") { CodeSettings settings = new CodeSettings { EvalTreatment = EvalTreatment.MakeImmediateSafe, MinifyCode = true, RemoveUnneededCode = true, LocalRenaming = LocalRenaming.CrunchAll, RemoveFunctionExpressionNames = true }; return cruncher.MinifyJavaScript(content, settings); } else if (extension == ".css") { return cruncher.MinifyStyleSheet(content); } return content; } } }
MVC4引入:WebGrease.dll
其它項目引入:AjaxMin.dll 下載地址:http://ajaxmin.codeplex.com