Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom HttpModule for IIS 7 for integrated

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.

like image 557
Jonas Verdickt Avatar asked May 06 '11 13:05

Jonas Verdickt


People also ask

What is the difference between HttpModule and HTTP handler?

HttpHandler is responsible for handling http request by extension while HttpModule is responding to application life cycle events. Save this answer.

What is the difference between middleware and HttpModule?

Unlike HttpModules, there is full control of what get's executed and in what order. As they are executed in the order in which they are added. Order of middleware for responses is the reverse from that for requests. Middleware is independent of these events.

Where do I put HttpModules in web config?

web>/<httpModules> configuration section. Instead, it should use the <system. webServer>/<modules> configuration section to load ASP.NET module components.

How do I add a managed module in IIS?

In the Home pane, double-click Modules. In the Actions pane, click Add Managed Module. In the Add Managed Module dialog box, enter the name of the managed module in the Name box, and then enter or select the module's .


1 Answers

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.

like image 146
Kev Avatar answered Sep 22 '22 00:09

Kev