压缩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压缩的应用相关推荐

  1. ASP.net mvc开发中使用纯html如何创建FCKeditor编辑器的使用

    http://www.cnblogs.com/esshs/archive/2008/12/03/1346326.html FCKeditor下载路径: http://sourceforge.net/p ...

  2. 探索HTTP传输中gzip压缩的秘密

    探索HTTP传输中gzip压缩的秘密 为什么要开启gZip 我们给某人发送邮件时,我们在传输之前把自己的文件压缩一下,接收方收到文件后再去解压获取文件.这中操作对于我们来说都已经司空见惯.我们压缩文件 ...

  3. 如何压缩css代码,在开发中怎么压缩js和css?有哪些办法?

    在开发的时候我们会选择将自己的代码进行压缩和打包,那么对于"在开发中怎么压缩js和css?有哪些办法?"这个问题小编为带来了一些干货. 对于压缩 js 与 css,我们一般是使用在 ...

  4. 浅谈实际开发中常用的分布式事物处理

    浅谈实际开发中常用的分布式事物处理 文章目录 前言 一.分布式事物 二.常用方案 1.使用记录表+mq机制 前言 随着微服务的流行,越来越多系统不在是单体结构,根据业务和功能拆分成不同微服务,这就导致 ...

  5. Nginx中Gzip压缩功能的实例配置

    Gzip压缩功能的实例配置 gzip on; #开启gzip功能 gzip_types *; #压缩源文件类型,根据具体的访问资源类型设定 gzip_comp_level 6; #gzip压缩级别 g ...

  6. tomcat中gzip压缩

    在tomcat中压缩文件,修改server.xml文件中的配置 <Connector port="8080" protocol="HTTP/1.1"con ...

  7. c#实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩

    全栈工程师开发手册 (作者:栾鹏) c#教程全解 c#实现gzip压缩解压缩byte[]字节数组,文件,字符串. 测试代码 static void Main() {//测试字符串String inpu ...

  8. java实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩

    全栈工程师开发手册 (作者:栾鹏) java教程全解 java实现gzip压缩解压缩byte[]字节数组,文件,字符串. 测试代码 public static void main(String[] a ...

  9. 浅谈游戏开发中逻辑与表现的分离

    回顾之前做的几个Demo,做点总结. 一.做法: 为了更清晰,NRatel将 一个游戏对象类 拆分为 两个类,如下: 1.定义"纯粹的逻辑类".   基本职责:对游戏对象的&quo ...

最新文章

  1. 为何 Map接口不继承Collection接口
  2. oracle不使用游标,oracle – 为什么我们不能在动态SQL语句中使用强引用游标?
  3. rm删除文件显示:Operation not permitted
  4. 自动躲避障碍物,微型蜂鸟机器人靠AI算法飞行
  5. 程序员们怎么过端午?你属于哪一款?
  6. oracle 创建用户
  7. Python_base_正则表达式
  8. VsCode 配置java环境(详细教程)
  9. 手提无法使用Ghost方法安装win7系统,出现一直显示DOS工具箱和硬盘无效情况的解决
  10. 关于java.lang.IllegalArgumentException: KFC Crazy Thursday need $50的问题
  11. 393高校毕业设计选题
  12. 程序员深度体验一周ChatGPT发现竟然....
  13. Wattagio for Mac(电池管理)
  14. 《大规模元搜索引擎技》——第1章 绪言1.1 Web上查找信息
  15. 数字图像处理(DIP)实验4 目标颜色识别
  16. 删除一个字符串中的所有数字字符
  17. 基于zynq的千兆网udp项目_基于FPGA的千兆网UDP通信分析
  18. 【网络信息安全】PKI 技术
  19. matlab数值积分的计算
  20. vue中监听div的滑动到底部 ,并实现懒加载

热门文章

  1. Css实现的鼠标滑动选项卡菜单代码
  2. 修改注册表添加信任站点及启用Activex控件(转载)
  3. Sublime Text 2
  4. redis配置主从复制
  5. Docker for Linux 安装
  6. [BZOJ 5093]图的价值
  7. Looping over the databases on a server
  8. css:hover选择器
  9. wpf 客户端【JDAgent桌面助手】开发详解(四) popup控件的win8.0的bug
  10. 在Java 7里如何对文件进行操作