压缩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. iOS开发中的压缩以及解压

    事实上,在iOS开发中,压缩与解压,我都是采用第三方框架SSZipArchive实现的 gitHub地址:   https://github.com/ZipArchive/ZipArchive 上面有 ...

  2. tomcat中gzip压缩

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

  3. mvc开发中DTO,DO,FROM的区别

    DO:数据库实体类映射到model里的实体类,每个字段都和数据库相对应,一般来说开发的时候不要去添加或者修改里面的实体 DTO:与前台交互的时候(一般来说是查询操作)有一些数据字段是那一张表里面没有囊 ...

  4. 浅谈iOS开发中多语言的字符串排序

    一.前言 在iOS开发中,一个经常的场景是利用tableview展示一组数据,以很多首歌曲为例子.为了便于查找,一般会把这些歌曲按照一定的顺序排列,还会加上索引条以便于快速定位. 由于歌曲名可能有数字 ...

  5. ASP.NET MVC开发中常见异常及解决方案

    ASP.NET MVC4入门到精通系列目录汇总 NHibernate:no persister for 异常 1.配置文件后缀名写错 mapping file 必须是.hbm.xml结尾 2.Web. ...

  6. ASP.NET MVC 开发中遇到的两个小问题

    最近在做一个网站,用asp.net MVC4.0来开发,今天遇到了两个小问题,通过查找相关渠道解决了,在这里把这两个问题写出来,问题非常简单,不喜勿喷,mark之希望可以给遇到相同问题的初学者一点帮助 ...

  7. 2014-07-29 浅谈MVC框架中Razor与ASPX视图引擎

    今天是在吾索实习的第15天.随着准备工作的完善,我们小组将逐步开始手机端BBS的开发,而且我们将计划使用MVC框架进行该系统的开发.虽然我们对MVC框架并不是非常熟悉,或许这会降低我们开发该系统的效率 ...

  8. 浅谈Web开发中的定时任务

    曾经做过Windows server下的定时任务的业务,最近又做了一些Linux下使用Crontab做的定时任务的业务,觉得有必要进行一次小结,于是有了如下这篇文章. Windows Server下 ...

  9. MVC开发中的常见错误-02-在应用程序配置文件中找不到名为“OAEntities”的连接字符串。

    在应用程序配置文件中找不到名为“OAEntities”的连接字符串. 分析原因:由于Model类是数据库实体模型,通过从数据库中引用的方式添加实体,所以会自动产生一个数据库连接字符串,而程序运行到此, ...

随机推荐

  1. Leetcode 65 Valid Number 字符串处理

    由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...

  2. css之选择器篇

    css能够获取到HTML结构上的元素,这个是怎么实现的了? 在我们看来这是个很神奇的事情,css可以写在页面之外,也可以写在页面内,而都不会影响到它去 获取这个元素,还有无论这个HTML结构多么复杂, ...

  3. aehyok.com的成长之路一——开篇

    前言   不得不说最近三个月都没更新博客了,除了6月初的一篇博客外,今天的这一篇算是这三个月里发表的第二篇博客了.不过本人几乎每天都在博客园里刷来刷去,看大家发表的博文,从中汲取营养.确实博客园也可以 ...

  4. Python:常用函数封装

    def is_chinese(uchar): """判断一个unicode是否是汉字""" if uchar >= u'\u4e00' ...

  5. windows地址转发

    netsh interface portproxy add v4tov4 listenport=8080 connectaddress=192.168.8.108 connectport=8080 把 ...

  6. sublime Text Pastry使用

    来源:   https://github.com/duydao/Text-Pastry/wiki/Examples Using a text list Using the Clipboard Clip ...

  7. 基于Qt的遥感图像处理软件设计总结

     开发工具 VS2008+Qt4.8.0+GDAL1.9  要点 接口要独立,软件平台与算法模块独立,平台中各接口设计灵活,修改时容易. 设计软件时一步步来,每个功能逐一实现,某个功能当比较独立时可以 ...

  8. 超体.特效中英字幕.Lucy.2014.BD1080P.X264.AAC.English&Mandarin.CHS-ENG

    资源名称 其它信息 资源大小 BT下载 超体.Lucy.2014.BD-MP4-原创翻译中英双语字幕.mp4 seeders: / leechers: 511.15MB 下载 [飘域家园]移动迷宫.T ...

  9. THinkphp开启静态(动态)缓存的用法

    <?php return array( //开启静态缓存 'HTML_CACHE_ON' => true, 'HTML_CACHE_RULES' => array( 'News:in ...

  10. 微信公众平台开发视频教程-03-获取Access Token和获取微信服务器IP,添加微信菜单

    1 获取access token 此token是以后每次调用微信接口都会带上的票据,token是公众号全局唯一票据,在调用其他接口之前都需要先得到token,taoken长度至少512个字符,通常用s ...