谈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();
         }
谈mvc开发中gzip压缩的应用的更多相关文章
- iOS开发中的压缩以及解压
		
事实上,在iOS开发中,压缩与解压,我都是采用第三方框架SSZipArchive实现的 gitHub地址: https://github.com/ZipArchive/ZipArchive 上面有 ...
 - tomcat中gzip压缩
		
在tomcat中压缩文件,修改server.xml文件中的配置 <Connector port="8080" protocol="HTTP/1.1" co ...
 - mvc开发中DTO,DO,FROM的区别
		
DO:数据库实体类映射到model里的实体类,每个字段都和数据库相对应,一般来说开发的时候不要去添加或者修改里面的实体 DTO:与前台交互的时候(一般来说是查询操作)有一些数据字段是那一张表里面没有囊 ...
 - 浅谈iOS开发中多语言的字符串排序
		
一.前言 在iOS开发中,一个经常的场景是利用tableview展示一组数据,以很多首歌曲为例子.为了便于查找,一般会把这些歌曲按照一定的顺序排列,还会加上索引条以便于快速定位. 由于歌曲名可能有数字 ...
 - ASP.NET MVC开发中常见异常及解决方案
		
ASP.NET MVC4入门到精通系列目录汇总 NHibernate:no persister for 异常 1.配置文件后缀名写错 mapping file 必须是.hbm.xml结尾 2.Web. ...
 - ASP.NET MVC 开发中遇到的两个小问题
		
最近在做一个网站,用asp.net MVC4.0来开发,今天遇到了两个小问题,通过查找相关渠道解决了,在这里把这两个问题写出来,问题非常简单,不喜勿喷,mark之希望可以给遇到相同问题的初学者一点帮助 ...
 - 2014-07-29 浅谈MVC框架中Razor与ASPX视图引擎
		
今天是在吾索实习的第15天.随着准备工作的完善,我们小组将逐步开始手机端BBS的开发,而且我们将计划使用MVC框架进行该系统的开发.虽然我们对MVC框架并不是非常熟悉,或许这会降低我们开发该系统的效率 ...
 - 浅谈Web开发中的定时任务
		
曾经做过Windows server下的定时任务的业务,最近又做了一些Linux下使用Crontab做的定时任务的业务,觉得有必要进行一次小结,于是有了如下这篇文章. Windows Server下 ...
 - MVC开发中的常见错误-02-在应用程序配置文件中找不到名为“OAEntities”的连接字符串。
		
在应用程序配置文件中找不到名为“OAEntities”的连接字符串. 分析原因:由于Model类是数据库实体模型,通过从数据库中引用的方式添加实体,所以会自动产生一个数据库连接字符串,而程序运行到此, ...
 
随机推荐
- 从range和xrange的性能对比到yield关键字(上)
			
使用xrange 当我们获取某个数量的循环时,我们惯用的手法是for循环和range函数,例如: for i in range(10): print i 这里range(10)生成了一个长度为10 ...
 - 重学JAVA基础(七):线程的wait、notify、notifyAll、sleep
			
/** * 测试thread的wait notify notifyAll sleep Interrupted * @author tomsnail * @date 2015年4月20日 下午3:20: ...
 - Swift 必备开发库 (高级篇)
			
1.CryptoSwift swift加密库, 支持md5,sha1,sha224,sha256... github地址: https://github.com/krzyzanowskim/Crypt ...
 - WPF窗体的命令绑定
			
方法一:使用代码 <WpfUI:View.CommandBindings> <CommandBinding Command="Help" CanExecute=& ...
 - 《CSS 设计指南》学习笔记 一
			
本篇文章是对这几天看完 Charles Wyke-Smit 的 <CSS 设计指南> 后的一些学习笔记与心得,笔者好像是大一的时候开始接触网页设计,由于并不是计算机专业的,所以所有都是自己 ...
 - 为EasyUI 的Tab 标签添加右键菜单
			
在网上看了很多demo 自己实现了一个效果如下 ps jquery1.7.2 jQuery EasyUI 1.3.6easyui QQ群:15129679 <!doctype html> ...
 - ★android开发--ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)
			
例子中用于解析Json的Gson请自己Google下载 主Activity: package COM.Example.Main; import java.util.HashMap; import ja ...
 - Xcode 文档注释方法
			
摘自:http://www.cnblogs.com/bomo/p/4815963.html 文档注释,可以在调用时显示注释信息,让调用者更好的理解方法的用途. 注释方法: /// 注释 和 /** 注 ...
 - CHECK MEMBER TYPE
			
检查类里是否存在某种类型的几种方法,以检查xxx类型为例:方法1: template<class T> class has_member_type_Type { ]; }; templat ...
 - sql优化之(DMV)
			
原文地址:http://technet.microsoft.com/zh-cn/library/bb838723.aspx Microsoft SQL Server 2005 提供了一些工具来监控数据 ...