Error pages in ASP.NET

Posted by koevoeter on ASP.net Weblogs See other posts from ASP.net Weblogs or by koevoeter
Published on Tue, 18 May 2010 11:37:00 GMT Indexed on 2010/05/18 11:51 UTC
Read the original article Hit count: 442

In ASP.NET you can retrieve the last unhandled exception via:

(HttpContext.Current.)Server.GetLastError() // Server object is available as a property in Page and UserControl context

This obviously only works in the same roundtrip. If you want to retrieve this information in your error page, you got a problem because the error page is not returned in the same roundtrip. The server responds with a redirect response and a new request to the error page is automatically sent. A common workaround would be to store the exception in your Session state from the Application_Error event in Global.asax.

From ASP.NET 3.5 you can configure the redirect mode for error pages:

<customErrors mode="On" defaultRedirect="~/Error.aspx" redirectMode="ResponseRewrite" />

This way the redirect response is not sent and the error page is returned right away. That implies that the browser is not aware of a page change and cannot reflect it in the address bar, so your original URL is not replaced with the URL of the error page, which might be what you actually want…

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about error handling