user1079703
user1079703

Reputation: 492

ASP.net - Custom Error pages reporting on the error

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

Upvotes: 1

Views: 328

Answers (2)

JasonD
JasonD

Reputation: 1016

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>

Upvotes: 1

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

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
}

Upvotes: 1

Related Questions