HttpModule doesn't run on 404 errors RRS feed

  • Question

  • User1030464876 posted


    I have written a class that implements the IHttpModule to log and redirect requests between two domains. It works perfectly when I run the application on localhost. But it doesn't seem to run on my web hosting if the requested page doesn't exists (I get the regular "The page cannot be found" 404 error message). If the page does exists, it runs as i should. Anyone got any ideas?

    Here is my config file ( /Web.config ):

        <appSettings />
        <connectionStrings />
        <compilation debug="false" />
        <authentication mode="Windows" />
          <add name="RequestHandlerModule" type="RequestHandlerModule" />

    Here is my class file ( /App_Code/RequestHandlerModule.cs ):

    using System;
    using System.IO;
    using System.Web;
    public class RequestHandlerModule : IHttpModule
        public RequestHandlerModule()
        public void Init(HttpApplication application)
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
            application.EndRequest += (new EventHandler(this.Application_EndRequest));
        private void Application_BeginRequest(Object source, EventArgs e)
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            // Log request
            // If requested file does not exist redirect ...
        private void Application_EndRequest(Object source, EventArgs e)
        public void Dispose()



    // Martin 

    Thursday, April 19, 2007 7:33 AM

All replies

  • User-225114762 posted
    If you're using the development server in Visual Studio 2005, and the files you're trying to handle are without extensions (or not handled by ASP.NET, such as .gif), you need to enable wild-card mapping in IIS to get the requests to be handled by ASP.NET at all. The development server (Cassini) handles all requests always, which is why it works in that scenario - if this indeed is your situation.
    Thursday, April 19, 2007 8:23 AM
  • User1030464876 posted


    I see. What if I make a request to http://www.mydomain.com/folderThatDoesNotExist/ , should that be handled by .net? Is there a way to do that?


    Thursday, April 19, 2007 9:02 AM
  • User-225114762 posted
    Yep. It won't normally, but if you want it to,  enable Wild Card mapping, and disable "check that file exists".
    Thursday, April 19, 2007 9:41 AM