asp.net-mvc – Gzip压缩无法运行ASP.net MVC5
发布时间:2020-05-22 17:25:13 所属栏目:asp.Net 来源:互联网
导读:我想用Gzip压缩我的Web应用程序,我正在使用以下类 压缩过滤器 public class CompressFilter : ActionFilterAttribute{ public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpRequestBase r
|
我想用Gzip压缩我的Web应用程序,我正在使用以下类 压缩过滤器 public class CompressFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(acceptEncoding)) return;
acceptEncoding = acceptEncoding.ToUpperInvariant();
HttpResponseBase response = filterContext.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);
}
}
}
缓存过滤器 public class CacheFilterAttribute : ActionFilterAttribute
{
public int Duration
{
get;
set;
}
public CacheFilterAttribute()
{
Duration = 1;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (Duration <= 0) return;
HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
TimeSpan cacheDuration = TimeSpan.FromMinutes(Duration);
cache.SetCacheability(HttpCacheability.Public);
cache.SetExpires(DateTime.Now.Add(cacheDuration));
cache.SetMaxAge(cacheDuration);
cache.AppendCacheExtension("must-revalidate,proxy-revalidate");
}
}
调节器 [CompressFilter]
[CacheFilter(Duration = 60)]
public ActionResult Index()
{}
并将此类应用于Controller中的Action.但是在firebug中,它仍然显示“Transfer-Encoding:chunked”,但它应该是“Transfer-Encoding:gzip”. 我在localhost上测试它. 请告诉我,我做错了什么?感谢致敬. 更新缓存过滤器工作正常,但仍然没有gzip压缩,下面是chrome中的响应头. Cache-Control:public,must-revalidate,proxy-revalidate,max-age=3600 Content-Type:text/html; charset=utf-8 Date:Wed,22 Jul 2015 13:39:06 GMT Expires:Wed,22 Jul 2015 14:39:04 GMT Server:Microsoft-IIS/10.0 Transfer-Encoding:chunked X-AspNet-Version:4.0.30319 X-AspNetMvc-Version:5.1 X-Powered-By:ASP.NET X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcQXJiYXpcRG9jdW1lbnRzXFZpc3VhbCBTdHVkaW8gMjAxM1xQcm9qZWN0c1xidXlwcmljZXNwYWtpc3RhblxCdXlQaG9uZQ==?= 有什么方法可以让我的工作,我真的需要帮助的人,谢谢 解决方法如果您无法控制IIS,只需将其添加到Global.ASCX即可.在Android,iPhone和大多数PC浏览器上测试过.protected void Application_BeginRequest(object sender,EventArgs e)
{
// Implement HTTP compression
HttpApplication app = (HttpApplication)sender;
// Retrieve accepted encodings
string encodings = app.Request.Headers.Get("Accept-Encoding");
if (encodings != null)
{
// Check the browser accepts deflate or gzip (deflate takes preference)
encodings = encodings.ToLower();
if (encodings.Contains("deflate"))
{
app.Response.Filter = new DeflateStream(app.Response.Filter,CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding","deflate");
}
else if (encodings.Contains("gzip"))
{
app.Response.Filter = new GZipStream(app.Response.Filter,"gzip");
}
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .net – 使用服务而不是组件有什么优缺点?
- asp.net-mvc-3 – _AppStart正在执行时无法创建存储范围
- asp.net-mvc – 如何使用ViewBag创建一个下拉列表?
- asp.net-mvc – ASP.Net MVC在回发之间保持动作参数
- ide – 我如何处理必须编写经典ASP的代码?
- ASP.NET Web应用程序(MVC)部署自动化和Subversion
- asp.net – 如何在没有文件扩展名的情况下制作ASPX网页?
- asp.net-mvc – 如何在ASP.Net MVC中对自定义ActionFilter进
- 电子邮件 – 邮箱不可用,客户端没有权限作为此发件人发送 –
- asp.net核心 – ASP – 启动时核心迁移EF核心SQL DB
推荐文章
站长推荐
- asp.net-mvc-3 – MVC3不显眼的验证在IE中不起作
- asp.net-mvc-3 – Asp.Net MVC 3 – @ Html.Acti
- asp.net – 在MVC3或IIS 7.5中禁用x-frame-optio
- asp.net-mvc – 跨多个页面的MVC3 RenderPartial
- asp.net – 在所选数据源上找不到具有该名称的字
- Azure Apps EasyAuth声称使用.NET Core
- asp.net-mvc – 如何将锚/哈希的参数添加到Redir
- asp.net – Page_Load中的Response.Redirect
- 让OData和NHibernate结合进行动态查询
- asp.net-mvc – 我应该在Web应用程序中记录哪些信
热点阅读
