Welcome to MSDN Blogs Sign in | Join | Help

Rahul Soni's blog

Never assume the obvious is true!

News



  • These postings are provided "AS IS" with no warranties, and confers no rights.
Customize errors which are not captured by customErrors in ASP.NET 2.0

Yesterday, I came up with an interesting issue where a customer wanted to customize a 500 error message ("Internal Server Error"). Interestingly, customErrors DID NOT seem to work for StatusCode 500! We just wanted to verify if it works for 404, and it did...

<customErrors defaultRedirect="~/ErrorPage.aspx" mode="On">
            <error statusCode="500" redirect="~/ErrorPage.aspx" />
            <error statusCode="404" redirect="~/ErrorPage.aspx" />
</customErrors>

Ideally, this should have worked... but may be it didn't because some errors are not customizable... http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/80cb8d8d-8fd8-4af5-bb3b-4d11fff3ab9c.mspx?mfr=true

Let us reproduce the error in just a few steps and then we will fix it by creating an HttpModule to handle our scenario.

1. Create a new ASP.NET web service using VB.NET (or C#)
2. Set Service.asmx as start page.
3. Browse to http://localhost/CustomError/Service.asmx?WSDL => This should work
4. Now, browse to http://localhost/CustomError/Service.asmx?WSDL=12 => This should fail with an error code 500 and description "Internal Server Error"

So, now since we are able to repro this error pretty easily, we will start the next step of fixing this and ensure that whenever this happens, we are redirected appropriately to the ErrorPage.aspx.

1. Start by creating a custom page called ErrorPage.aspx under your project folder directly.
2. Under the App_Code folder create a VB class file called ErrorModule.vb and paste the following code...

Imports Microsoft.VisualBasic
Public Class ErrorModule
    Implements IHttpModule
    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
        'Hmmm... let it be!
    End Sub
    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
        AddHandler context.EndRequest, AddressOf ErrorRedirector
    End Sub
    Public Sub ErrorRedirector(ByVal sender As Object, ByVal e As EventArgs)
        Dim myApp As HttpApplication
        Dim myContext As HttpContext
        Try
            myApp = CType(sender, HttpApplication)
            myContext = myApp.Context
            If myContext.Response.StatusCode.ToString = 500 Then
                myContext.Response.Redirect("ErrorPage.aspx")
            End If
        Catch ex As Exception
            'I don't wanna catch it
        Finally
            myApp = Nothing
            myContext = Nothing
        End Try
    End Sub
End Class

3. In your web.config file paste the following code...

  <httpModules>
   <add name="MyErrorModule" type="ErrorModule"/>
  </httpModules>

4. Compile your project

Now, if you open your browser and browse to http://localhost/CustomError/Service.asmx?WSDL=12, even though you get a 500 error from IIS, you should be redirected to http://localhost/CustomError/ErrorPage.aspx

Yes, it is that simple :o)

Cheers,
Rahul

kick it on DotNetKicks.com
Posted: Thursday, January 18, 2007 5:01 PM by rahulso

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# January 18, 2007 6:55 AM

rahulso said:

By the way, I forgot to add this link...

Displaying Gentle Error Messages with ASP.NET ->  

http://msdn.microsoft.com/coding4fun/web/misc/article.aspx?articleid=1378666&title=Displaying+Gentle+Error+Messages+with+ASP.NET

# January 18, 2007 7:05 AM

Mohamed Yehia - Microsoft said:

if you thought that all server error can be handled by custom errors in ASP.Net, think again!! ...

# January 18, 2007 10:40 AM

Chris Love's Official Blog - Professional ASP.NET said:

I have been pretty busy lately and the last week has flown by on me. I have been spending some of my

# January 18, 2007 3:03 PM

Mads said:

Odd that CustomErrors can't catch a 500 code.

Well, glad you posted it.

# January 19, 2007 4:33 AM

rahulso said:

Yes Mads, I agree!

Honestly speaking even I don't know the root cause it. If I come to know about it anyhow, I will definitely post it!

Thanks for your comments though :)

Cheers,

Rahul

# January 19, 2007 5:37 AM

said:

You can do this in the Application_Error method in Global.asax file and needn't use a separate HttpModule for it.

Shiva

# January 19, 2007 9:00 AM

rahulso said:

Hi Shivap...

Can you please share the code related to Application_Error?

Thanks,

Rahul

# January 19, 2007 10:41 AM

mike's web log said:

Had an unusual one today. (For me, anyway.) I have a little app at work that for various reasons has to impersonate an identity. For the time being, I do this with this element in the Web.config file:

# January 19, 2007 10:56 PM

Chris Love's Official Blog - Professional ASP.NET said:

I have been pretty busy lately and the last week has flown by on me. I have been spending some of my

# June 26, 2008 5:22 PM

Shivani said:

The above HttpModule doesnot catch unhandled SQLException(if SQLServer is stopped).Please try this and if you have some solution than please post it here.

# October 4, 2008 2:40 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker