HttpModule 在 IIS 5.1 中不拦截 js 和 css 文件

发布于 2024-10-17 05:43:57 字数 1388 浏览 1 评论 0原文

我正在实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李白 2024-10-24 05:43:57

在 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.

非要怀念 2024-10-24 05:43:57

我会确保 .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

沉鱼一梦 2024-10-24 05:43:57

您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文