谈mvc开发中gzip压缩的应用
压缩view的内容,可加过滤器
public class GzipFilter : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
string acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
if (String.IsNullOrEmpty(acceptEncoding)) return;
var response = filterContext.HttpContext.Response;
acceptEncoding = acceptEncoding.ToUpperInvariant();
if (acceptEncoding.Contains("GZIP"))
{
response.AppendHeader("Content-Encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.AppendHeader("Content-Encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
然后在要压缩的页面控制器上加标签。
[GzipFilter]
public ActionResult Index()
现在基本上所有的浏览器支持gzip, deflate.
这里是编程对css和js文件进行压缩放在本地,然后发送给客户端。
----这种方法在iis7.5的集成模式下有效,在vs中有效,但在iis6里我还没配置好,无效
----关键是请求,只对action有效,像js,css文件的请求,在BeginRequest里检测不到。这种方法运行在iis7里很完美,文件大概会被压缩到原来的1/3到1/4.
此方法主要是给请求的文件加上http头//Response.AppendHeader("Content-Encoding", "gzip"); 这里很难处理。
如果有谁找到iis6里面可以运行的方法麻烦告诉我,或许能一起讨论找到更好的解决方案,非常感谢!
---pukuimin@qq.com
浏览器检测到这个头,就会对文件进行解压缩,就正常运行了。
protected void Application_BeginRequest(object sender, EventArgs e)
{
GzipFiles();
}
private void GzipFiles()
{
string acceptEncoding = Request.Headers["Accept-Encoding"];
string filepath = Request.FilePath;
string mapfilepath = Server.MapPath("~" + filepath);
if (acceptEncoding.Contains("gzip"))
{
#region Gzip处理
if (filepath.EndsWith(".css"))//css文件处理
{
Response.AppendHeader("Content-Type", "text/css");
Request.ContentType = "text/css";
if (filepath.EndsWith("gzip.css"))
{
FileInfo fi = new FileInfo(mapfilepath);
Response.AppendHeader("Content-Encoding", "gzip");
int len = mapfilepath.Length - "gzip.css".Length;
if (fi.Exists == false) GZip(mapfilepath.Substring(0, len), mapfilepath);
}
}
else if (filepath.EndsWith(".js"))//js文件处理
{
Response.AppendHeader("Content-Type", "application/x-javascript");
Request.ContentType = "application/x-javascript";
if (filepath.EndsWith("gzip.js"))
{
FileInfo fi = new FileInfo(mapfilepath);
Response.AppendHeader("Content-Encoding", "gzip");
int len = mapfilepath.Length - "gzip.js".Length;
if (fi.Exists == false) GZip(mapfilepath.Substring(0, len), mapfilepath);
}
}
#endregion
}
else if (acceptEncoding.Contains("deflate"))
{
#region deflate处理
if (filepath.EndsWith(".css"))//css文件处理
{
Response.AppendHeader("Content-Type", "text/css");
Request.ContentType = "text/css";
if (filepath.EndsWith("deflate.css"))
{
FileInfo fi = new FileInfo(mapfilepath);
Response.AppendHeader("Content-Encoding", "gzip");
int len = mapfilepath.Length - "deflate.css".Length;
if (fi.Exists == false) GZip(mapfilepath.Substring(0, len), mapfilepath);
}
}
else if (filepath.EndsWith(".js"))//js文件处理
{
Response.AppendHeader("Content-Type", "application/x-javascript");
Request.ContentType = "application/x-javascript";
if (filepath.EndsWith("deflate.js"))
{
FileInfo fi = new FileInfo(mapfilepath);
Response.AppendHeader("Content-Encoding", "gzip");
int len = mapfilepath.Length - "deflate.js".Length;
if (fi.Exists == false) GZip(mapfilepath.Substring(0, len), mapfilepath);
}
}
#endregion
}
}
public void GZip(string fileName, string gipFileName)
{
FileStream fr = File.Create(gipFileName);
FileStream fc = File.OpenRead(fileName);
GZipStream gzs = new GZipStream(fr, CompressionMode.Compress); //压缩文件类
byte[] arr = new byte[fc.Length];
fc.Read(arr, 0, (int)fc.Length);
gzs.Write(arr, 0, (int)fc.Length);
gzs.Close();
fc.Close();
fr.Close();
}
//解压缩文件方法
public void DeZGip(string fileName, string gipFileName)
{
//准备输入输出文件
FileStream fc = File.Create(fileName);
FileStream fr = File.OpenRead(gipFileName);
GZipStream gzs = new GZipStream(fr, CompressionMode.Decompress);
byte[] arr = new byte[fr.Length];
fr.Read(arr, 0, (int)fr.Length);
fc.Write(arr, 0, (int)fr.Length);
gzs.Close();
fr.Close();
fc.Close();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)