Rod*_*nho 7 caching gzip minify asp.net-mvc-3
抱歉我的英语不好,但我想这不会有问题.我只是想分享一个很好的Helper类,我使用Microsoft Ajax Minifier对我们的脚本和样式进行组合,缩小和gzip.在开始之前,请下载ICSharpCode.SharpZipLib.这是一个使用gzip的开源库.
让我们从web.config开始(我将专注于IIS7).在这里,我们向我们的应用程序说,对cssh或jsh扩展所做的任何请求都将被转发到MinifierHelper类.我选择使用这些扩展(cssh和jsh),如果我们想要,不管出于什么原因,不要缩小特定的脚本或样式,按照你正常使用的方式使用它.
<system.webServer>
<handlers>
<remove name="ScriptHandler" />
<remove name="StyleHandler" />
<add name="ScriptHandler" verb="*" path="*.jsh" type="Site.Helpers.MinifierHelper" resourceType="Unspecified" />
<add name="StyleHandler" verb="*" path="*.cssh" type="Site.Helpers.MinifierHelper" resourceType="Unspecified" />
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
我使用文件夹脚本和样式来存储文件.我不使用Visual Studio建议的文件夹内容.
下一步是配置global.asax.我们必须告诉我们的应用程序不要路由这些文件夹.将这些行添加到RegisterRoutes方法中.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("Scripts/{*path}");
routes.IgnoreRoute("Styles/{*path}");
...
}
Run Code Online (Sandbox Code Playgroud)
好.现在我将展示如何使用我们的课程.在视图中:
<link href="/Styles/Folder/File.cssh" type="text/css" rel="stylesheet" />
<script src="/Scripts/Folder/File.jsh" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
在我的示例中,我将所有脚本和样式的逻辑设置为脚本/样式中的文件夹内部.例如:站点 - >脚本 - >主页 - > index.css.我使用与脚本和样式的视图相同的结构.例如:站点 - >视图 - >主页 - > index.cshtml.如果需要,您可以更改此模式.
现在制作魔法的代码:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;
using ICSharpCode.SharpZipLib.GZip;
using Microsoft.Ajax.Utilities;
namespace Site.Helpers
{
public abstract class MinifierBase : IHttpHandler, IRequiresSessionState
{
#region Fields
private HttpContext context;
private byte[] responseBytes;
private bool isScript;
protected string fileName;
protected string folderName;
protected List<string> files;
#endregion
#region Properties
public bool IsReusable
{
get { return false; }
}
#endregion
#region Methods
public static string setUrl(string url)
{
var publishDate = ConfigurationManager.AppSettings["PublishDate"];
return url + "h" + ((publishDate != null) ? "?id=" + publishDate : "");
}
public void ProcessRequest(HttpContext context)
{
this.context = context;
this.isScript = context.Request.Url.PathAndQuery.Contains("/Scripts/");
this.process();
}
private void process()
{
if (this.context.Request.QueryString.HasKeys())
{
string url = this.context.Request.Url.PathAndQuery;
if (this.context.Cache[url] != null)
{
this.responseBytes = this.context.Cache[url] as byte[];
}
else
{
this.writeResponseBytes();
this.context.Cache.Add
(
url,
this.responseBytes,
null,
DateTime.Now.AddMonths(1),
Cache.NoSlidingExpiration,
CacheItemPriority.Low,
null
);
}
}
else
{
this.writeResponseBytes();
}
this.writeBytes();
}
private void writeResponseBytes()
{
using (MemoryStream ms = new MemoryStream(8092))
{
using (Stream writer = this.canGZip() ? (Stream)(new GZipOutputStream(ms)) : ms)
{
var sb = new StringBuilder();
var regex = new Regex(@"^/.+/(?<folder>.+)/(?<name>.+)\..+");
var url = regex.Match(this.context.Request.Path);
var folderName = url.Groups["folder"].Value;
var fileName = url.Groups["name"].Value;
this.getFileNames(fileName, folderName).ForEach(delegate(string file)
{
sb.Append(File.ReadAllText(this.context.Server.MapPath(file)));
});
var minifier = new Minifier();
var minified = string.Empty;
if (this.isScript)
{
var settings = new CodeSettings();
settings.LocalRenaming = LocalRenaming.CrunchAll;
settings.OutputMode = OutputMode.SingleLine;
settings.PreserveImportantComments = false;
settings.TermSemicolons = true;
minified = minifier.MinifyJavaScript(sb.ToString(), settings);
}
else
{
var settings = new CssSettings();
settings.CommentMode = CssComment.Important;
settings.OutputMode = OutputMode.SingleLine;
minified = minifier.MinifyStyleSheet(sb.ToString(), settings);
}
var bts = Encoding.UTF8.GetBytes(minified);
writer.Write(bts, 0, bts.Length);
}
this.responseBytes = ms.ToArray();
}
}
private List<String> getFileNames(string fileName, string folderName = "")
{
this.files = new List<String>();
this.fileName = fileName;
this.folderName = folderName;
if (folderName == "Global" && fileName == "global-min")
{
if (this.isScript) this.addGlobalScripts();
else this.addDefaultStyles();
}
else
{
var flags = BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance;
var mi = this.GetType().GetMethod
(
"add" +
this.folderName +
CultureInfo.CurrentCulture.TextInfo.ToTitleCase(fileName).Replace("-", "") +
(this.isScript ? "Scripts" : "Styles"),
flags
);
if (mi != null)
{
mi.Invoke(this, null);
}
else
{
if (this.isScript) this.addDefaultScripts();
else this.addDefaultStyles();
}
}
return files;
}
private void writeBytes()
{
var response = this.context.Response;
response.AppendHeader("Content-Length", this.responseBytes.Length.ToString());
response.ContentType = this.isScript ? "text/javascript" : "text/css";
if (this.canGZip())
{
response.AppendHeader("Content-Encoding", "gzip");
}
else
{
response.AppendHeader("Content-Encoding", "utf-8");
}
response.ContentEncoding = Encoding.Unicode;
response.OutputStream.Write(this.responseBytes, 0, this.responseBytes.Length);
response.Flush();
}
private bool canGZip()
{
string acceptEncoding = this.context.Request.Headers["Accept-Encoding"];
return (!string.IsNullOrEmpty(acceptEncoding) && (acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate")));
}
protected abstract void addGlobalScripts();
protected abstract void addGlobalStyles();
protected abstract void addDefaultScripts();
protected abstract void addDefaultStyles();
#endregion
}
Run Code Online (Sandbox Code Playgroud)
那是基类.现在我们将创建继承自基类的Helper类.这是我们选择应添加哪些脚本的类.
public class MinifierHelper : MinifierBase
{
#region Methods
Run Code Online (Sandbox Code Playgroud)
要合并和缩小全局脚本/样式,请将当前行添加到View:
<link href="@MinifierHelper.setUrl("/Styles/Global/global-min.css")" type="text/css" rel="stylesheet" />
<script src="@MinifierHelper.setUrl("/Scripts/Global/global-min.js")" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
它将在我们的MinifierHelper类中调用addGlobalScripts/addGlobalStyles方法.
protected override void addGlobalScripts()
{
this.files.Add("~/Scripts/Lib/jquery-1.6.2.js");
this.files.Add("~/Scripts/Lib/jquery-ui-1.8.16.js");
this.files.Add("~/Scripts/Lib/jquery.unobtrusive-ajax.js");
this.files.Add("~/Scripts/Lib/jquery.validate.js");
...
}
protected override void addGlobalStyles()
{
this.files.Add("~/Styles/Global/reset.css");
this.files.Add("~/Styles/Global/main.css");
this.files.Add("~/Styles/Global/form.css");
...
}
Run Code Online (Sandbox Code Playgroud)
要缩小特定脚本/样式(特定于页面),请将当前行添加到视图中:
<link href="@MinifierHelper.setUrl("/Styles/Curriculum/index.css")" type="text/css" rel="stylesheet" />
Run Code Online (Sandbox Code Playgroud)
MinifierHelper类将尝试查找名为"add"+ FolderName + FileName +"Styles"的方法.在我们的例子中,它将寻找addCurriculumIndexStyles.在我的例子中它存在,因此将触发该方法.
public void addCurriculumIndexStyles()
{
this.files.Add("~/Styles/Global/curriculum.css");
this.files.Add("~/Styles/Curriculum/personal-info.css");
this.files.Add("~/Styles/Curriculum/academic-info.css");
this.files.Add("~/Styles/Curriculum/professional-info.css");
}
Run Code Online (Sandbox Code Playgroud)
如果类没有找到该特定方法,它将触发默认方法.默认方法使用指定的相同文件夹/名称缩小脚本/样式.
protected override void addDefaultScripts()
{
this.files.Add("~/Scripts/" + this.folderName + "/" + this.fileName + ".js");
}
protected override void addDefaultStyles()
{
this.files.Add("~/Styles/" + this.folderName + "/" + this.fileName + ".css");
}
Run Code Online (Sandbox Code Playgroud)
别忘了关闭该地区和班级.
#endregion
}
Run Code Online (Sandbox Code Playgroud)
而已.我希望你们都明白了.
我忘了告诉最后一件事.在web.config中,在AppSettings中添加名为PublishDate的键.我在值中输入了一个包含完整日期和时间的字符串(例如:261020111245).目标是独特.此密钥将用于缓存我们的缩小脚本.如果您不创建此密钥,您的应用程序将不使用缓存.我建议使用这个.因此,每次更新脚本/样式时,也要更新PublishDate.
<add key="PublishDate" value="261020111245" />
Run Code Online (Sandbox Code Playgroud)
Mindscape Web Workbench 是完成您正在寻找的项目的绝佳工具。
http://visualstudiogallery.msdn.microsoft.com/2b96d16a-c986-4501-8f97-8008f9db141a
这是一篇关于它的好博客文章:
http://visualstudiogallery.msdn.microsoft.com/2b96d16a-c986-4501-8f97-8008f9db141a