HttpModule 在 IIS 5.1 中不拦截 js 和 css 文件
我正在实现 HttpModule 来压缩请求。 下面是 HttpModule 的代码:
public class Global : IHttpModule
{
public void Init(HttpApplication app)
{
app.PostReleaseRequestState += new EventHandler(app_PostReleaseRequestState);
}
void app_PostReleaseRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
// If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
if (acceptEncoding.Contains("gzip"))
{
// Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding.Contains("deflate"))
{
// Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "deflate");
}
}
它能够在开发 Web 服务器中拦截和压缩 js 和 css,但是当我从 IIS 5.1 运行它时 它无法压缩js和css文件。 请帮忙。
I am implementing HttpModule for compressing request.
Below is the codee for HttpModule:
public class Global : IHttpModule
{
public void Init(HttpApplication app)
{
app.PostReleaseRequestState += new EventHandler(app_PostReleaseRequestState);
}
void app_PostReleaseRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
// If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
if (acceptEncoding.Contains("gzip"))
{
// Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding.Contains("deflate"))
{
// Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "deflate");
}
}
It's able to intercept and compress js and css in the development web server but when i run it from IIS 5.1
it is not able to compress js and css files.
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 IIS7 之前,将非 ASP.NET 文件类型放入 ASP.NET 管道需要设置文件类型映射以通过 ISAPI 重定向这些文件。如果您将
*.js
和*.css
映射为由 ISAPI 处理,您的代码应该开始针对这些请求运行。这是 在 IIS6 中执行此操作的示例(尽管您需要替换
*.js
和*.css 为
*.asp
)。如果我没记错的话,5.1 的管理界面非常相似,ScottGu 的示例应该仍然有帮助。Prior to IIS7, getting non-ASP.NET file types into the ASP.NET pipeline requires setting up a file type mapping to redirect those files through ISAPI. If you map
*.js
and*.css
to be processed by ISAPI, your code should start running for those requests.Here's an example of doing that in IIS6 (though you'll want to substitute
*.js
and*.css
for*.asp
). If I remember correctly, 5.1's management interface is similar enough that ScottGu's example should still be helpful.我会确保 .js 和 .css 文件由 .NET 框架处理。
IIS 7 及更高版本的参考可以在 iis.net/ConfigReference/system.webServer/handlers 中找到。
关于 IIS 6,您可以检查 js 和 css 是否在以下位置处理:
站点设置/主目录/应用程序设置/(应用程序池)配置/映射
I would make sure that .js and .css files are handled by the .NET framework.
The reference for IIS 7 and above can be found at iis.net/ConfigReference/system.webServer/handlers
Concerning IIS 6, you can check that js and css are handled under:
Site settings / Home Directory / Application Settings / (Application Pool) Configuration / Mappings
您使用 HttpModule 的方向是正确的。但是,为了更改 HTML 内容,您将使用 HttpApplication.PostReleaseRequestState 处理程序。为了更改资源文件,您将使用 HttpApplication.BeginRequest 处理程序。
You are on the right track using HttpModule. However, in order to change HTML contents, you would use the HttpApplication.PostReleaseRequestState handler. In order to change resource files, you would use the HttpApplication.BeginRequest handler.