Custom HttpModule for IIS 7 for integrated
Asked Answered
E

3

8

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.

Elfredaelfrida answered 6/5, 2011 at 13:10 Comment(0)
I
3

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.

Ible answered 7/5, 2011 at 0:58 Comment(4)
Thanks for the fast response, I did set the application pool to managed pipeline mode. I tried it out using IIS express on my local machine, and when that didn't work, I published the site to an server with IIS 7. There I could see the module beeing loaded, but it is never fired. So I'm gessing its something else I'm doing wrong...Elfredaelfrida
A syntax error in my web.config and some clean/build solution and publishes later, it seems to work...Elfredaelfrida
@jonas could you expand upon the web.config issues you found? I'm experiencing a similar problem and wondering if I have the same problem you had.Toddtoddie
@Toddtoddie Sorry but what I did was basically some trial and error, so I don't really remember what I did to solve this issue...Elfredaelfrida
B
0

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.

Ballesteros answered 16/5, 2012 at 15:21 Comment(0)
Q
0

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>
Questor answered 7/8, 2012 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.