【问题标题】:HttpModule does not intercept js and css files in IIS 5.1HttpModule 不拦截 IIS 5.1 中的 js 和 css 文件
【发布时间】:2011-06-26 23:09:29
【问题描述】:

我正在实现 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 文件。 请帮忙。

【问题讨论】:

    标签: c# asp.net httpmodule


    【解决方案1】:

    我会确保 .js 和 .css 文件由 .NET 框架处理。

    可以在 iis.net/ConfigReference/system.webServer/handlers 找到 IIS 7 及更高版本的参考

    关于 IIS 6,您可以检查 js 和 css 是否在以下条件下处理: 站点设置/主目录/应用程序设置/(应用程序池)配置/映射

    【讨论】:

    • 但是可以在web develoer 2008的开发web服务器中拦截。
    【解决方案2】:

    在 IIS7 之前,将非 ASP.NET 文件类型导入 ASP.NET 管道需要设置文件类型映射以通过 ISAPI 重定向这些文件。如果您将 *.js*.css 映射为由 ISAPI 处理,您的代码应该开始为这些请求运行。

    这里是an example of doing that in IIS6(尽管您需要将*.js*.css 替换为*.asp)。如果我没记错的话,5.1 的管理界面非常相似,ScottGu 的示例应该仍然有用。

    【讨论】:

    • 我们需要为此编写 HttpHandler 还是只需添加文件类型就可以了。
    • 只需添加文件类型。然后,这些类型的文件将开始通过您的 HttpModules 运行。
    • 确保将这些扩展路由到 ISAPI。如果不出意外,请考虑尝试“通配符”映射,将现有的*.aspx 映射更改为*.*。这样您就可以确保分配了正确的处理程序。
    【解决方案3】:

    您在使用 HttpModule 时走在正确的轨道上。但是,为了更改 HTML 内容,您将使用 HttpApplication.PostReleaseRequestState 处理程序。为了更改资源文件,您将使用 HttpApplication.BeginRequest 处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 2011-09-06
      • 1970-01-01
      • 2015-11-07
      • 2016-01-14
      相关资源
      最近更新 更多