Handling unhandled exceptions from AJAX.Net postback
Posted
Monday, November 05, 2007 9:02 PM
by
ysa
A couple of days ago, I had to deal with a bug in a page I wrote. The page contains a couple of UpdatePanel controls. For some reason, when an exception was thrown during the PostBack of one of this panels, the page was not redirected to our error page (which is the case for all none AJAX postbacks), but it's message is displayed in an alert popup. This is not very good - it a security hole that can expose internal information (The bug was detected when my team leader, Doron, got a message which contains a SQL statement which failed to run). Further more, if an exception is not redirected to the error page it was not handled - our error page contains the logging of the unhandled exceptions.
So I started to dig in.
I found out that unhandled exceptions in the AJAX.Net framework are handled by an event in the ScriptManager : "AsyncPostBackError". You can register to this event, handle the exception (log ,etc...), and override the error message which will be displayed (If this wasn't done already by EntLib) by setting the property "AsyncPostBackErrorMessage" to the message that you want to display.
In our case, because we are using a Master page for the pages in our site, I registered to the event in the Master page, handled it with EntLib, and replaced the message (in case the EntLib configuration haven't done that already) :
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
ExceptionUtils.Handle(e.Exception);
ScriptManager1.AsyncPostBackErrorMessage = "exception handled";
}
In this way, I didn't had to replicate the handling code in all of our AJAX pages.
Use it wisely.