简体   繁体   English

使用ASP.NET 4.0和IIS7进行HTTP压缩

[英]HTTP Compression with ASP.NET 4.0 and IIS7

I've been trying to implement HTTP compression on a few of my sites for some time now. 我一直试图在我的一些网站上实现HTTP压缩一段时间。

I've tried implementing HTTP compression using variations on the following two techniques: 我尝试使用以下两种技术的变体实现HTTP压缩:

From Application_BeginRequest in Global.asax 来自Global.asax中的Application_BeginRequest

From a custom HttpModule 来自自定义的HttpModule

In both cases, it initially seems to work. 在这两种情况下,它最初似乎都有效。 But, as I navagate around the site, I quickly lose my CSS styling. 但是,当我在网站上浏览时,我很快就失去了我的CSS样式。 And the brower reports a JS file contains invalid characters. 并且浏览器报告JS文件包含无效字符。 And, at some point, the entire site turns entirely to jibberish. 而且,在某些时候,整个网站完全变成了胡言乱语。

I sure wish I knew what was happening here. 我当然希望我知道这里发生了什么。 Does anyone have any suggestion? 有没有人有任何建议? Is it because these techniques only work on older versions of ASP.NET/IIS? 是因为这些技术仅适用于旧版本的ASP.NET / IIS吗?

EDIT: I do not have access to IIS, and should not require it. 编辑:我没有IIS的访问权限,不应该要求它。 With the latest iterations of ASP.NET and IIS, more and more control is being given to web.config without touching IIS. 随着ASP.NET和IIS的最新迭代,越来越多的控制权被赋予web.config而不涉及IIS。 This should be fully supported without delving into IIS and, in fact, the two links above claim they are able to do just this. 这应该完全支持,而不是深入研究IIS,事实上,上面的两个链接声称他们能够做到这一点。 I just can't get it to work for me. 我无法让它为我工作。

EDIT: On further inspection, using the code in the second link above (the HTTP module), I can see the module handler is being called for all file types on my site. 编辑:进一步检查,使用上面第二个链接(HTTP模块)中的代码,我可以看到我的网站上的所有文件类型都在调用模块处理程序。 This is clearly part of the problem. 这显然是问题的一部分。 (Note that, in some cases, the entire page turns to garbage, which suggests other problems.) Can anyone suggest what determines which files are sent to the HTTP handler, or why the code at that link might work for someone else but not me? (请注意,在某些情况下,整个页面变为垃圾,这表明存在其他问题。)任何人都可以建议确定将哪些文件发送到HTTP处理程序的内容,或者为什么该链接上的代码可能对其他人有效而不是我?

IIS 7 supports compression natively - there should not be any coding required. IIS 7本身支持压缩 - 不应该进行任何编码。 Unless, perhaps, you are trying to implement it on a hosting provider that has not enabled it, but that is not made clear in your question 除非您尝试在未启用它的托管服务提供商上实现它,但在您的问题中并未明确说明

While I may have had a few issues going on here, the following quote from Walkthrough: Creating and Registering a Custom HTTP Module seems to illuminate the problem: 虽然我可能在这里遇到了一些问题,但演练:创建和注册自定义HTTP模块的以下引用似乎说明了这个问题:

If the ASP.NET application is running under IIS 6.0, you can use HTTP modules to customize requests for resources that are serviced by ASP.NET. 如果ASP.NET应用程序在IIS 6.0下运行,则可以使用HTTP模块自定义对ASP.NET提供服务的资源的请求。 This includes ASP.NET Web pages (.aspx files), Web services (.asmx files), ASP.NET handlers (.ashx files), and any file types that you have mapped to ASP.NET. 这包括ASP.NET网页(.aspx文件),Web服务(.asmx文件),ASP.NET处理程序(.ashx文件)以及已映射到ASP.NET的任何文件类型。 If the ASP.NET application is running under IIS 7.0, you can use HTTP modules to customize requests for any resources that are served by IIS. 如果ASP.NET应用程序在IIS 7.0下运行,则可以使用HTTP模块自定义对IIS提供的任何资源的请求。 This includes not just ASP.NET resources, but HTML files (.htm or .html files), graphics files, and so on. 这不仅包括ASP.NET资源,还包括HTML文件(.htm或.html文件),图形文件等。

As I was beginning to suspect, this is a change in ASP.NET 4.0 and IIS7. 正如我开始怀疑的那样,这是ASP.NET 4.0和IIS7的变化。 As I've mentioned several times, these are becoming more and more integrated. 正如我多次提到的,这些正在变得越来越集成。 And the quote above suggests that HTTP modules now handle all files types served by IIS. 上面的引用表明HTTP模块现在处理IIS提供的所有文件类型。

So that's why the older code doesn't work for me. 这就是为什么旧代码对我不起作用的原因。 And the fix appears to be to simply check the file type type from my handler. 修复似乎只是从我的处理程序中检查文件类型类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM