views:

103

answers:

3

Basically I want to take the following: alt text

And make it match the styling of the rest of the application.

I am creating a custom error page in my C# based project and I want it to be able to show the same information that is displayed in the ASP.NET default error page. From fiddling with reflector I can see that this is generated through HttpException.GetHtmlErrorMessage() but when I try to use this in my exception it returns null.

A: 

I've never tried doing this but all IIS errors are recorded to the event log. You could try to read the last error from the event log and display that if it is already written.

You'd also want to add a filter to your event grabbing to ensure you're showing events from your application and that there aren't other error events within a significant amount of time.

Paul Mendoza
This is of course tricky, especially if you're running the app on a shared server. Not only do I doubt you'd have access to the event log, but also another application could have logged another event in the time between the error occurring and retrieving an event entry. Actually, your own app could also do that.
ErikHeemskerk
You don't need to add a filter to do event grabbing, just handle Application_Error in global.asax.cs. Server.GetLastError( ) will have the exception information.
Dan Vallejo
As Erik says, there is no way to know for sure what error in the log to display--no way to know which error is now causing this particular user of this app to see the error page.
apollodude217
+1  A: 

You don't need to add a filter to do event grabbing, just handle Application_Error in global.asax.cs. Server.GetLastError( ) will have the exception information

Yeah, erm... No. The error shown in the original question shows a parsing/compilation error - these errors happen in the HttpHandler pipeline for ASP.NET (ISAPI Filter in older IIS versions) i.e. before your application is event started, so before any of the events in Global.asax.

Although you can specify a custom error page (in web.config, machine.config, or IIS metabase), these can only be HTML files.

1) if you're only interested in exceptions which arise in your code (i.e. your code compiles, but then an exception is thrown) then you can use Dan's suggestion from above and handle the Application_Error event in Glocal.asax.

If you want to handle ASP.NET exceptions (e.g. Parsing/Compilation errors, config files errors, etc) then you'll need to hook in (or replace) the ASP.NET HttpHandler.

You could wrap the existing handler by writing your own, and catching any exceptions, then redirecting to another error page.

You'd specify your handler in your web.config file (or machine.config if it's a global handler).

There are some good tutorials on the web on how to do this. Try starting here: http://msdn.microsoft.com/en-us/library/f3ff8w4a(VS.71).aspx

(main problem is: to catch parsing/compilation errors you need to write a handler/filter which is a level above the ASP.NET handler/filter (I believe)).

Hope this helps, Dourn.

Dourn
+1  A: 

Iain,

I used this code in order to do something similar on a custom error page. I'm not sure if showing the exact source code region that caused the error is possible using the Exception object, but I was able to use the stack trace, which includes line numbers and method names:

If Not IsPostBack Then
    Dim ex As Exception = Server.GetLastError().GetBaseException()
    lblExceptionMessage.Text = ex.Message.ToString()
    lblStackTrace.Text = ex.StackTrace().Replace(System.Environment.NewLine, "<br />")
End If

You can also use ex.TargetSite to get just the method name that threw the exception.

HTH,

Mike

Mike C