1

I'm trying to set it up so that when an error is thrown in my asp.net site, the resulting custom error page returns an identifier, which can be used to look an error up in the database.

The custom error page is set up in the web.config using the "customErrors" functionality.

  <customErrors defaultRedirect="~/Error/error.aspx" mode="RemoteOnly">
      <error statusCode="404" redirect="~/Error/error.aspx?status=404" />
  </customErrors>

I'm currently bridging the gap using the session to pass along the id.

In Global.asax

    protected void Application_Error(Object sender, EventArgs e)
    {
        Exception ex = HttpContext.Current.Server.GetLastError();
        var errorCode = ExceptionManager.Publish(ex);
        Session["ERROR_IDENTIFIER"] = errorCode;
    }

In the CustomError page:

<%@ Page Language="C#" Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Error Page</title>
  <link href="/Error/css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <%
     string errorIdentifier = Session["ERROR_IDENTIFIER"] == null ? "this is null" : Session["ERROR_IDENTIFIER"].ToString();
     Response.Write("There's been an error. Please send this identifier to service desk -> " + errorIdentifier);
  %>
</body>
<html>

Unfortunately, what seems to happen in this workflow is that the customError page loads faster than the global.asax runs. Basically these are two distinct operations and they're being run separately, and I'd like to make it so that the custom error page has the correct Identifier in it.

Thanks, Alex

user1079703
  • 312
  • 4
  • 13

2 Answers2

1

There may be another solution to this Ans or may be this will be. My suggestion is to not use custom error handling in this case

<customErrors mode="Off" defaultRedirect="~/Error/error.aspx"/>

if you are looking for exact identifier then you must be redirect error page from global.ascx file, so that you can get exact Identifier for error since on every error or exception Application_Error method will get executed.

protected void Application_Error(Object sender, EventArgs e)
{
    Exception ex = HttpContext.Current.Server.GetLastError();
    var errorCode = ExceptionManager.Publish(ex);
    Session["ERROR_IDENTIFIER"] = errorCode;
    Response.Redirect("~/Error/error.aspx");//Added
}
Jaydip Jadhav
  • 11,638
  • 6
  • 18
  • 36
  • Yeah, I was thinking about this as well, but then I would have to rewrite the custom Off|On|RemoteOnly logic in the Application_Error global, which I wasn't too particularly keen on doing. – user1079703 Jul 22 '16 at 17:14
1

See this SO Question: ASP.NET custom error page - Server.GetLastError() is null

You are redirecting to the error page and losing the error information in the Application_Error handler.

TL;DR:

Change you web config to look like this:

   <customErrors defaultRedirect="~/Error/error.aspx" mode="RemoteOnly">
      <error statusCode="404" redirect="~/Error/error.aspx?status=404" redirectMode="ResponseRewrite"  />
  </customErrors>
Community
  • 1
  • 1
JasonD
  • 986
  • 1
  • 5
  • 5
  • Not sure what's up with this, but it's looking like my session variable isn't making it over anymore. I turned on tracing and there are no session variables even though I know they're getting set. – user1079703 Jul 22 '16 at 21:48