1

For a website/ vpath, it's possible to handle the Application_Error event to catch errors before they get sent back to the browser. Is it possible to also do this at the server level somehow? That is, define a method at the root level that will execute if an error occurs in a website, but that website fails to handle the error for whatever reason.

I know you can use the web.config at the root level to define custom error messages per HTTP status code. However, this isn't ideal for my case, because I want to return different types of content (ie, HTML or something else) depending on the application logic.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260

2 Answers2

1

A custom http module can be registered at applicationHost.config. Then this module is used by all IIS applications on the target machine.

1) Create a signed class library project with http module:

public class ErrorHandlingModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.Error += new EventHandler(context_Error);
    }

    void context_Error(object sender, EventArgs e)
    {
        // handle error
    }
}

2) Install the class library into GAC, so it can be shared by all IIS applications.

3) Install the http module to applicationHost.config file. This file usualy resides in C:\Windows\System32\inetsrv\config. Files in this folder can be accessed only by 64-bit processes (there is no such issue on 32-bit OSes), VS2010 cannot see them but Explorer can. The applicationHost.config fragment could look like this:

<location path="" overrideMode="Allow">
    <system.webServer>
        <modules>
            <add name="MyModule" preCondition="managedHandler" type="GlobalErrorHandler.ErrorHandlingModule, GlobalErrorHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bfd166351ed997df" />
Community
  • 1
  • 1
Jakub Linhart
  • 4,062
  • 1
  • 26
  • 42
  • Thanks, this is exactly what I was looking for! One follow-up question, if you wouldn't mind: if I have this module and also handle Application_Error in global.asax, which would get called first? – McGarnagle Mar 27 '12 at 07:37
  • Glad it helped. In my case http module is called first. But I don't think it is guaranteed at least I can't find any documentation that is mentioning execution order of application's event handlers. Please let me know, if you find something about this topic:). – Jakub Linhart Mar 27 '12 at 08:46
0

I am not clear what your question is but as per my understanding. inside application_error you can use ,

Server.GetLastError() to get last error occured in server level.
Vetrivel mp
  • 1,214
  • 1
  • 14
  • 29
  • Updated the question for clarity. My question is whether you can have a code block that catches any errors that it's websites fail to catch. So effectively like a global.asax application_on_error for the whole server, rather than for each individual application. – McGarnagle Mar 20 '12 at 04:52
  • i think you can only see in event viwer. – Vetrivel mp Mar 21 '12 at 06:46