In ASP.NET when you are in DEBUG
mode and something fails you get a the famous yellow screen of death.
It shows that there was a Server Error in <Location> Application
, provides a description, exception details, source file and stack trace.
I would like to extend this error page to include some extra information.
The error page formatting is done by System.Web.ErrorFormatter
and its subclasses, which unfortunately are internal. You can grab the HTML and modify it if you like using HttpException.GetHtmlErrorMessage()
. But I'd be inclined to do as other commenters have suggested and use Server.GetLastError
and format it myself.
If so - please ensure you HtmlEncode
the error message output to mitigate XSS and ensure your production sites don't display any information about the error (as this was the attack vector for the ASP.NET padding oracle decryption attack in 2010).
This page seems to have the info you're looking for.
http://www.asp.net/hosting/tutorials/processing-unhandled-exceptions-cs
Essentially, when an exception bubbles up to to the runtime it gets wrapped in an System.Web.HttpException
(This was wrong. At least I didn't see this behavior in MVC 3).
I'd think you can extend that class and override GetHtmlErrorMessage to return whatever extra data you want.
Update: Well, with a little experimentation it appears I'd be wrong. BUT there is a workaround...
You can write the info from the exception in any manner you choose then clear the error and you'll have what you want returned.
Something like this...
protected void Application_Error(object sender, EventArgs e)
{
var context = HttpContext.Current;
var error = context.Server.GetLastError();
context.Response.Write(error.Message);
context.Response.Write("<br /><br />");
context.Response.Write(error.StackTrace);
context.Response.Write("<br /><br />");
context.Response.Write("Hello World");
context.Server.ClearError();
}
If you are using custom error pages, I believe you can get the last error with HttpContext.Current.Server.GetLastError();
I'm not sure if there are any custom pages out there. But the content of the YSOD should be available from the context of the request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With