14

I have created an error page to show a general message for all unhandled exceptions.

This is the code in Global.asax

HttpContext ctx = HttpContext.Current;

            string e404_PAGE = ctx.Request.AppRelativeCurrentExecutionFilePath.ToString();
            string e404_LINE = ctx.Server.GetLastError().InnerException.StackTrace.Substring(ctx.Server.GetLastError().InnerException.StackTrace.LastIndexOf(":line ") + 6, ctx.Server.GetLastError().InnerException.StackTrace.Substring(ctx.Server.GetLastError().InnerException.StackTrace.LastIndexOf(":line ") + 6).IndexOf(" ")).ToString();
            string e404_MESSAGE = ctx.Server.GetLastError().InnerException.Message.ToString();
            string e404_METHODNAME = ctx.Server.GetLastError().InnerException.TargetSite.ToString();
            string e404_STACKTRACE = ctx.Server.GetLastError().InnerException.StackTrace.ToString();
            string e404_URL = ctx.Request.Url.ToString();
            string e404_DATE = ctx.Timestamp.ToString("yyyy-MM-dd HH:mm:ss");
            string e404_USER = ctx.User.Identity.Name.ToString();
            string e404_IP = ctx.Request.UserHostAddress.ToString();

            // * * * //

            System.Data.SqlClient.SqlConnection sql_conn;
            sql_conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
            sql_conn.Open();
            string query = @"insert into UnhandledExceptions(Message, Page, Line, MethodName, StackTrace, URL, Date, [User], IP) 
                                         values(@Message, @Page, @Line, @MethodName, @StackTrace, @URL, @Date, @User, @IP)
                                         select scope_identity();";
            System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand(query, sql_conn);
            com.Parameters.AddWithValue("@Message", e404_MESSAGE);
            com.Parameters.AddWithValue("@Page", e404_PAGE);
            com.Parameters.AddWithValue("@Line", e404_LINE);
            com.Parameters.AddWithValue("@MethodName", e404_METHODNAME);
            com.Parameters.AddWithValue("@StackTrace", e404_STACKTRACE);
            com.Parameters.AddWithValue("@URL", e404_URL);
            com.Parameters.AddWithValue("@Date", e404_DATE);
            com.Parameters.AddWithValue("@User", e404_USER);
            com.Parameters.AddWithValue("@IP", e404_IP);

            string e404_ID = com.ExecuteScalar().ToString();

            sql_conn.Close();

            // * * * //            

            Session["e404_ID"] = e404_ID;
            Response.Redirect("~/Error.aspx");

When I publish the website, the user is not being redirected to the error page.

All the code until the last line works fine. Any suggestion?

  • where in your Global.asax is this code? – Luis Tellez Feb 11 '13 at 22:54
  • @LuisTellez the code is inside Application_Error –  Feb 11 '13 at 22:56
  • 1
    Just pointing out the obvious: You've stated that `Response.Redirect` doesn't work.. and yet you use `Server.Transfer`. Also, have you tried a direct URL instead? (without the tilde) – Simon Whitehead Feb 11 '13 at 22:57
  • @SimonWhitehead the thing is that i have folders and subfolders in my application, so i need an absolute path for the Error.aspx page, which is just under the root. –  Feb 11 '13 at 22:59
  • Do you have a `customErrors` attribute in your `web.config`? – Simon Whitehead Feb 11 '13 at 23:00
  • @SimonWhitehead no, it's commented. –  Feb 11 '13 at 23:00
  • Are you receiving an error? Or it's just not doing anything? What if you do actually use response.redirect.. does the browser do anything? – Simon Whitehead Feb 11 '13 at 23:02
  • @SimonWhitehead It executes all the code, including the setting of the session and then it stops. I receive the error of the original page. I get the very same result with redirect. I think the problem is the path of the error page... –  Feb 11 '13 at 23:08
  • why all the downvotes without posting a comment first? –  Feb 11 '13 at 23:31

1 Answers1

36

Replace Response.Redirect("~/Error.aspx"); with:

// You've handled the error, so clear it. Leaving the server in an error state can cause unintended side effects as the server continues its attempts to handle the error.
Server.ClearError();

// Possible that a partially rendered page has already been written to response buffer before encountering error, so clear it.
Response.Clear();

// Finally redirect, transfer, or render a error view
Response.Redirect("~/Error.aspx");
Sybeus
  • 1,169
  • 11
  • 18
  • 4
    hey i have use the same code above but still it gives the error Response is not available in this context. – Rhushikesh Sep 24 '13 at 07:50
  • 1
    This seems to have sorted out an odd situation I had where the it was logging the error, getting to (and apparently executing) the redirect but still sending the default error page to the browser (along with a 500 response in the IIS log). – Daz Jan 21 '14 at 13:07