ASP.NET MVC3 Web应用程序中启用GZip压缩示例
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压缩示例的更多相关文章
- 在 ASP.NET MVC Web 应用程序中输出 RSS Feeds
RSS全称Really Simple Syndication.一些更新频率较高的网站可以通过RSS让订阅者快速获取更新信息.RSS文档需遵守XML规范的,其中必需包含标题.链接.描述信息,还可以包含发 ...
- web传输过程中的gzip压缩
最近在做项目的时候用到了gzip,发现它的压缩能力还是很强大的,基本能够压缩50%的文本文件大小.以前有所了解,但不够深入,现在详细了解下. 什么是gzip 在哪里使用gzip gzip对于不同类型文 ...
- 怎样在Nginxserver中启用Gzip压缩
原文链接: Enable GZIP Compression on nginx Servers原文日期: 2014年7月16日翻译日期: 2014年7月19日翻译人员: 铁锚 速度决定一切,没有什么比一 ...
- IIS中启用gzip压缩(网站优化)
HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中的 ...
- 【Web优化】Yslow优化法则(四)启用Gzip压缩
Yslow的第4个经验法则指出:启用gzip压缩功能,能够降低HTTP传输的数据和时间,从而降低client请求的响应时间. 本篇是Yslow法则的第四个,主要包含三个方面的内容: 1. 什 ...
- 在IIS上启用Gzip压缩(HTTP压缩)
一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 二.前言. 本文的知识点是从互联网收集整理, 主要来源于中文wiki. 使用Y ...
- ASP.NET Core Web 应用程序系列(五)- 在ASP.NET Core中使用AutoMapper进行实体映射
本章主要简单介绍下在ASP.NET Core中如何使用AutoMapper进行实体映射.在正式进入主题之前我们来看下几个概念: 1.数据库持久化对象PO(Persistent Object):顾名思义 ...
- ASP.NET Core Web 应用程序系列(三)- 在ASP.NET Core中使用Autofac替换自带DI进行构造函数和属性的批量依赖注入(MVC当中应用)
在上一章中主要和大家分享了在ASP.NET Core中如何使用Autofac替换自带DI进行构造函数的批量依赖注入,本章将和大家继续分享如何使之能够同时支持属性的批量依赖注入. 约定: 1.仓储层接口 ...
- ASP.NET Core Web 应用程序系列(二)- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)
在上一章中主要和大家分享在MVC当中如何使用ASP.NET Core内置的DI进行批量依赖注入,本章将继续和大家分享在ASP.NET Core中如何使用Autofac替换自带DI进行批量依赖注入. P ...
随机推荐
- strstr,strchr,strtr 比较
strstr - 查找字符串的首次出现 查找 "Shanghai" 在字符串中的第一次出现,并返回字符串的剩余部分: echo strstr("I love Shangh ...
- modelsim 仿真时出现无限迭代(iteration reach limitation)的原因及其解决办法
modelsim 仿真时出现无限迭代(iteration reach limitation) 出现这种故障的原因: 一般都是代码里出现的组合逻辑无限循环或者组合逻辑A产生signal_A,signa ...
- 用js实现跳转提示页面
效果图: 网页布局 <p>操作成功</p> <strong>5</strong><span>秒后回到主页</span><a ...
- 项目开发笔记-传单下发 名片替换 文件复制上传/html静态内容替换/json解析/html解析
//////////////////////////// 注意: 此博客是个人工作笔记 非独立demo////////////////////////////////// .............. ...
- The Shapes of CSS
#square { width: 100px; height: 100px; background: red; } #rectangle { width: 200px; height: 100px; ...
- SDK 组件 Qupaisdk 启动出错,错误消息为 [Qupaisdk], the android stack error message is Fail to start the plugin, which is caused by Failed resolution of: Lcom/duanqu/qupai/recorder/R$array;
紧急解决办法: 将你的oneSDK(qupaiSDK)里面的manifest的pageckage改为:com.duanqu.qupai.recorder
- Drbd 安装配置
一.Drbd介绍 Distributed Replicated Block Device(DRBD)是基于块设备在不同的高可用服务器之间同步和镜像数据的软件,通过它可以实现在网络中两台服务器这间基于块 ...
- 再议Unity 3D
一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...
- OC2_数组操作
// // main.m // OC2_数组操作 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxuemi ...
- UVA - 572 Oil Deposits(dfs)
题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...