用于集成 IIS 7 的自定义 HttpModule

发布于 2024-11-05 15:22:26 字数 1791 浏览 3 评论 0原文

我在构建自定义错误处理程序时遇到了麻烦。它应该是一个 HttpModule,但是当我将其添加到 web.configsystem.webServer/modules 标记时,它没有启动。

这是我的 web.config 部分:

<system.webServer>
  <modules>
    <add name="AspExceptionHandler" 
         type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" 
         preCondition="managedHandler" />
  </modules>
</system.webServer>

这是我的 HttpModule 中的代码:

using System;
using System.Web;
using Company.Settings;
using System.Configuration;

namespace Company.Exceptions
{
  public class AspExceptionHandler : IHttpModule
  {
    public void Dispose() { }

    public void Init(HttpApplication application)
    {
      application.Error += new EventHandler(ErrorHandler);
    }

    private void ErrorHandler(object sender, EventArgs e)
    {
      HttpApplication application = (HttpApplication)sender;
      HttpContext currentContext = application.Context;

      // Gather information5
      Exception currentException = application.Server.GetLastError();
      String errorPage = "http://www.mycompaniesmainsite.com/error.html";

      HttpException httpException = currentException as HttpException;
      if (httpException == null || httpException.GetHttpCode() != 404)
      {          
        currentContext.Server.Transfer(errorPage, true);
      }
      else
      {
        application.Response.Status = "404 Not Found";
        application.Response.StatusCode = 404;
        application.Response.StatusDescription = "Not Found";
        currentContext.Server.Transfer(errorPage, true);
      }
    }
  }
}

有人可以向我解释一下我做错了什么,以及 IIS 7 集成托管管道模式如何作品?因为我找到的大多数答案都是关于为 IIS 6 配置 HttpModules 的。

I'm having troubles with a custom Error handler I built. It should be a HttpModule, but when I add it to my web.config's system.webServer/modules tag, it is not initiated.

This is my web.config section:

<system.webServer>
  <modules>
    <add name="AspExceptionHandler" 
         type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" 
         preCondition="managedHandler" />
  </modules>
</system.webServer>

This is the code in my HttpModule:

using System;
using System.Web;
using Company.Settings;
using System.Configuration;

namespace Company.Exceptions
{
  public class AspExceptionHandler : IHttpModule
  {
    public void Dispose() { }

    public void Init(HttpApplication application)
    {
      application.Error += new EventHandler(ErrorHandler);
    }

    private void ErrorHandler(object sender, EventArgs e)
    {
      HttpApplication application = (HttpApplication)sender;
      HttpContext currentContext = application.Context;

      // Gather information5
      Exception currentException = application.Server.GetLastError();
      String errorPage = "http://www.mycompaniesmainsite.com/error.html";

      HttpException httpException = currentException as HttpException;
      if (httpException == null || httpException.GetHttpCode() != 404)
      {          
        currentContext.Server.Transfer(errorPage, true);
      }
      else
      {
        application.Response.Status = "404 Not Found";
        application.Response.StatusCode = 404;
        application.Response.StatusDescription = "Not Found";
        currentContext.Server.Transfer(errorPage, true);
      }
    }
  }
}

Could someone please explain to me what I am doing wrong, and how IIS 7 Integrated Managed Pipeline Mode works? Because most of the answers I found are about configuring HttpModules for IIS 6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一瞬间的火花 2024-11-12 15:22:26

据我所知,你走在正确的道路上。您是否确保站点的应用程序池设置为托管管道模式?

此外,如果您使用内置的 Visual Studio Web 服务器 (Cassini) 对此进行测试,则 部分将被忽略。如果您希望从那里加载模块,则需要 IIS7 或 IIS7.5 Express。

From what I can see you're on the right track. Have you made sure your site's application pool is set to Managed Pipeline mode?

Also if you're testing this with the built in Visual Studio web server (Cassini) then the <system.webServer> section will be ignored. You'll need IIS7 or IIS7.5 Express if you want the module to load from there.

壹場煙雨 2024-11-12 15:22:26

我遇到了这个问题,发现不关闭 customErrors 会阻止处理程序触发。

即:在您的配置中这是必需的,以便在 HttpModule 中捕获错误事件:

<system.web>
    <customErrors mode="Off" />
</system.web>

I had this problem and discovered that not turning off customErrors prevented the handler from triggering.

ie: this is required in your config for the Error event to be captured in the HttpModule:

<system.web>
    <customErrors mode="Off" />
</system.web>
别靠近我心 2024-11-12 15:22:26

我遇到了处理程序未触发的相同问题,通过对上述代码进行以下更改帮助我解决了此问题。我没有创建新的事件处理程序,而是将具有相同签名的方法附加到该事件。

application.Error += ErrorHandler;

这对我有用,仍在分析这种在 IIS7 中附加处理程序的方式背后的原因。

I was experiencing the same problem of a handler that is not getting triggered, by doing following change to the above code helped me to resolve this issue. Instead of creating a new event handler I just attached the method with same signature to that event.

application.Error += ErrorHandler;

This works for me, still analyzing what is the reason behind this way attaching a handler works in IIS7.

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