I have an asp.net application and I am trying to handle custom exception using Application_Error. It works nicely but I have new requirement to set the error data in Session obj
The problem might that the AcquireRequestState event might not have fired when the exception was raised in your application, you will be able to set a session value only after this particular event is fired as this is the stage where the application state is set
I've just come across this problem myself and, after a couple of hours of messing around, managed to solve it. The issue is that Application_Error()
clears the session after executing as part of some sort of "cleanup" routine which you need to stop.
The solution I found is as follows:
Call Server.ClearError();
- this clears the last error from the application and stops the "cleanup" taking place, thus preserving session.
The (unwanted imho) side-effect of this is that it no longer performs the automatic redirect to the error page, so you need to explicitly call Response.Redirect("~/error.aspx");
So, something like this:
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect!
Response.Redirect("~/error.aspx");
}
If you don't want to hard-code the URL, you could grab the defaultRedirect
URL directly from the customerrors
section in the web.config
, which would give you something like this:
protected void Application_Error(object sender, EventArgs e)
{
// Grab the last exception
Exception ex = Server.GetLastError();
// put it in session
Session["Last_Exception"] = ex;
// clear the last error to stop .net clearing session
Server.ClearError();
// The above stops the auto-redirect - so do a redirect using the default redirect from the customErrors section of the web.config!
var customerrors = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration("/").GetSection("system.web/customErrors");
Response.Redirect(customerrors.DefaultRedirect);
}
You could use the Cache and an appropriate naming strategy for your key, like this:
protected void Application_Error(object sender, EventArgs e)
{
HttpContext.Current.Cache["error:" + Session.SessionID] = Server.GetLastError();
Response.Redirect("Error.aspx");
}
in the Error.aspx page you ca read it like this:
var error = Cache["error:" + Session.SessionID];
Check this, the problem is with the redirect call.
Don't redirect after setting a Session variable (or do it right)
Add a directive to the Global.asax file, Imports the System.Web Namespace
<%@ Import Namespace="System.Web" %>
void Application_Error(object sender, EventArgs e) {
string message = Server.GetLastError().Message;
Session["error"] = message;
Server.Transfer("Error.aspx");
}
Write the error message added in the sesion object in the Error.aspx page
protected void Page_Load(object sender, EventArgs e){
if (!this.IsPostBack)
Response.Write(Session["error"].ToString());
}