1

I have made a custom error page for my ASP.NET 4 application. I put the exception object in HttpContext.current.Session["CustomError"] but when the user is redirected to the error page HttpContext.current.Session["CustomError"] is null. I do it in CustomError class constructor like this:

public CustomError(enExceptionType ExceptionType) : base(ExceptionMessage(ExceptionType)) { 
    HttpContext.Current.Session["CustomError"] = this; 
}

when I step over the code Session["Error"] contains the error object. any idea?

UPDATE:

I removed custom error page from web.config and added this to glabal.asax:

void Application_Error(object sender, EventArgs e)
{
    if (Context.IsCustomErrorEnabled)
    {
        Response.Redirect("~/Error.aspx");
    }
}

by stepping through this function I noticed that when an exception is thrown this function is called two time, the first time Session["CustiomError"] contains the error object but the second time its null.

nima
  • 5,965
  • 4
  • 40
  • 55
  • What do you want to achieve with the custom error session, If you want to log it, you can use the application_error event in global asax – Ivo Apr 23 '11 at 08:46
  • I want to display the error message of custom error object on error page. – nima Apr 23 '11 at 08:47
  • Can you put the code where you assign the exception? – Cristian Boariu Apr 23 '11 at 08:55
  • I updated the post and added the code – nima Apr 23 '11 at 09:04
  • @nima Are you sure that the redirect is done AFTER you fill in the exception value? – Cristian Boariu Apr 23 '11 at 09:20
  • I have a breakpoint on the line that sets the session and one on the error page load and they are in correct order. Its not only the exception object that is cleared from session, all of the object in session are null in the error page. – nima Apr 23 '11 at 09:40

2 Answers2

0

Instead of using Response.redirect(URL) (which I assume you have in your code) use

Server.Transfer(URL)

or

Response.redirect(url, false)

Why Server.Transfer(url)?

Transferring to another page using Server.Transfer conserves server resources. Instead of telling the browser to redirect, it simply changes the "focus" on the Web server and transfers the request. This means you don't get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster.

Source here.

Please let me know if one of these works for you.

UPDATE:

If you use a web config setting can you try adding ResponseWrite value to redirectmode var?

<customErrors mode="RemoteOnly" defaultRedirect="~/errors/GeneralError.aspx" redirectMode="ResponseRewrite" />

If this is still not working I suggest to implement this (I've done it in my application to log the errors in log files (for me as admin) and present a generic error to the user).

Community
  • 1
  • 1
Cristian Boariu
  • 9,331
  • 12
  • 87
  • 159
  • I don't use Response.Redirect. I've defined the custom error page in web.config and user is redirected to the error page on exception automatically. – nima Apr 23 '11 at 10:07
  • When I add ResponseRewrite an exception is thrown when I want to access Session in the error page: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the \\ section in the application configuration. – nima Apr 23 '11 at 10:21
  • @nima If you have this: `` in your web.config, you should not be having an issue like this. (source http://www.google.ro/search?sourceid=chrome&ie=UTF-8&q=Session+state+can+only+be+used+when+enableSessionState+is+set+to+true#q=Session+state+can+only+be+used+when+enableSessionState+is+set+to+true&hl=ro&prmd=ivnsfd&ei=A6myTdCNK5DpOeTkmaUJ&start=10&sa=N&fp=b99cabe63cc63409 ) – Cristian Boariu Apr 23 '11 at 10:26
  • 1
    I have enableSessionState="true" in web.config and in the error page itself but the exception is thrown when I add redirectMode="ResponseRewrite". – nima Apr 23 '11 at 10:46
  • @nima Does this solution works for your last error? http://stackoverflow.com/questions/781861/customerrors-does-not-work-when-setting-redirectmode-responserewrite/2271728#2271728 – Cristian Boariu Apr 23 '11 at 10:51
  • No, I don't have a problem with that. My custom error page loads whenever an exception is thrown. – nima Apr 23 '11 at 11:04
  • @nima And now if you use Server.Transfer like I put in the post? – Cristian Boariu Apr 23 '11 at 11:48
  • If I use Server.Transfer instead of Response.Redirect I cannot access Session in the error page. – nima Apr 23 '11 at 14:48
0

This solved the problem, but I would appreciate it if someone tells me why :)

void Application_Error(object sender, EventArgs e)
{
    if (Context.IsCustomErrorEnabled)
    {
        Response.Redirect("~/Error.aspx");
        **Server.ClearError();**
    }
}
nima
  • 5,965
  • 4
  • 40
  • 55