http://www.mzwu.com/article.asp?id=3284

自定义一个筛选器,继承于GZipAttribute:

using System;
using System.IO.Compression;
using System.Web.Mvc;

namespace MvcApplication1
{
    public class GZipAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var acceptEncoding = context.HttpContext.Request.Headers["Accept-Encoding"];
            if (!string.IsNullOrEmpty(acceptEncoding))
            {
                acceptEncoding = acceptEncoding.ToLower();
                var response = context.HttpContext.Response;
                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);
                }
            }
        }
    }
}

方法一:在需要GZip的Action上使用GZipAttribute

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        [GZip]
        public ActionResult Index()
        {
            ViewBag.Message = "欢迎使用 ASP.NET MVC!";

return View();
        }

public ActionResult About()
        {
            return View();
        }
    }
}

方法二:使用全局过滤器(MVC3+支持)

Global.asax.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new GZipAttribute());
        }

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

}

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

ASP.NET MVC3 Web应用程序中启用GZip压缩示例的更多相关文章

  1. 在 ASP.NET MVC Web 应用程序中输出 RSS Feeds

    RSS全称Really Simple Syndication.一些更新频率较高的网站可以通过RSS让订阅者快速获取更新信息.RSS文档需遵守XML规范的,其中必需包含标题.链接.描述信息,还可以包含发 ...

  2. web传输过程中的gzip压缩

    最近在做项目的时候用到了gzip,发现它的压缩能力还是很强大的,基本能够压缩50%的文本文件大小.以前有所了解,但不够深入,现在详细了解下. 什么是gzip 在哪里使用gzip gzip对于不同类型文 ...

  3. 怎样在Nginxserver中启用Gzip压缩

    原文链接: Enable GZIP Compression on nginx Servers原文日期: 2014年7月16日翻译日期: 2014年7月19日翻译人员: 铁锚 速度决定一切,没有什么比一 ...

  4. IIS中启用gzip压缩(网站优化)

    HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中的 ...

  5. 【Web优化】Yslow优化法则(四)启用Gzip压缩

    Yslow的第4个经验法则指出:启用gzip压缩功能,能够降低HTTP传输的数据和时间,从而降低client请求的响应时间. 本篇是Yslow法则的第四个,主要包含三个方面的内容: 1.      什 ...

  6. 在IIS上启用Gzip压缩(HTTP压缩)

    一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 二.前言. 本文的知识点是从互联网收集整理, 主要来源于中文wiki.  使用Y ...

  7. ASP.NET Core Web 应用程序系列(五)- 在ASP.NET Core中使用AutoMapper进行实体映射

    本章主要简单介绍下在ASP.NET Core中如何使用AutoMapper进行实体映射.在正式进入主题之前我们来看下几个概念: 1.数据库持久化对象PO(Persistent Object):顾名思义 ...

  8. ASP.NET Core Web 应用程序系列(三)- 在ASP.NET Core中使用Autofac替换自带DI进行构造函数和属性的批量依赖注入(MVC当中应用)

    在上一章中主要和大家分享了在ASP.NET Core中如何使用Autofac替换自带DI进行构造函数的批量依赖注入,本章将和大家继续分享如何使之能够同时支持属性的批量依赖注入. 约定: 1.仓储层接口 ...

  9. ASP.NET Core Web 应用程序系列(二)- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)

    在上一章中主要和大家分享在MVC当中如何使用ASP.NET Core内置的DI进行批量依赖注入,本章将继续和大家分享在ASP.NET Core中如何使用Autofac替换自带DI进行批量依赖注入. P ...

随机推荐

  1. selendroid inspector xpth元素定位记录

    android自动化测试元素定位,目前发现appium官方的uiautomatorviewer一般的元素定位还行,但好多都找不到. 这个时候,可以考虑selendroid的inspector 官网:h ...

  2. SSRS生成报表

    使用程序运行Reporting Service自动生成文件,可以参数使用ReportExecutionService.Render方法进行处理. 1.     连接至Reporting Service ...

  3. C# 判断路径是否存在

    定义文件状态枚举:0-路径为空,1-存在文件,2-路径不为空,但文件不存在 public enum FileExsitStatus { NoPath=0, FileExsit=1, NoFile=2 ...

  4. Sql 基于列的Case表达式

    Case表达式可以用在 Select,update ,delete ,set,in,where ,order by,having子句之后, 只是case表达式不能控制sql程序的流程,只能作为基于列的 ...

  5. Appium Python Driver Api

  6. IOS 应用程序启动加载过程(从点击图标到界面显示)

    今天帮同事解决问题的时候发现,程序BUG是由加载过程引起的.所以当局部代码没有问题,但是程序一运行却总不是我们想要结果的时候,我们应该想想是不是因为我们忽略了试图加载过程的原因.下面我们用一个例子来简 ...

  7. 2016/7/6 神·CPU的人类极限在哪?

    额,这其实是个搞怪贴 #include<stdio.h>int main(void){ int i,k; for(i=0;;i++) { k=i+222222222; printf(&qu ...

  8. 7zS.sfx RunProgram with parameters

    Config.txt as below: Pay attention to this \"  ;!@Install@!UTF-8! RunProgram="setup.exe&qu ...

  9. 专题三、ArrayList遍历方式以及效率比较

    一.遍历方式 ArrayList支持三种遍历方式. 1.第一种,随机访问,它是通过索引值去遍历 由于ArrayList实现了RandomAccess接口,它支持通过索引值去随机访问元素. 代码如下: ...

  10. php 调用 webservice服务

    class data{ $a = 123; $b = 456; } //直接php的SoapClient类 $client = new SoapClient('http://xxx.com/xx.as ...