用于IIS 7的自定义HttpModule集成

Jon*_*ckt 8 asp.net iis-7 httpmodule integrated-pipeline-mode

我遇到了我构建的自定义错误处理程序的麻烦.它应该是a HttpModule,但当我将它添加到我web.configsystem.webServer/modules标签时,它不会被启动.

这是我的web.config部分:

<system.webServer>
  <modules>
    <add name="AspExceptionHandler" 
         type="Company.Exceptions.AspExceptionHandler, Company.Exceptions" 
         preCondition="managedHandler" />
  </modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

这是我的代码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);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释我做错了什么,IIS 7集成管理管道模式如何工作?因为我发现的大多数答案都是关于配置HttpModulesIIS 6的.

Kev*_*Kev 3

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

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