2013-02-11 12 views
10

Ho creato una pagina di errore per mostrare un messaggio generale per tutte le eccezioni non gestite.Response.Redirect non funziona in Global.asax

Questo è il codice di 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"); 

Quando pubblico il sito web, l'utente non viene reindirizzato alla pagina di errore.

Tutto il codice fino all'ultima riga funziona correttamente. Qualche suggerimento?

+0

dove nel tuo Global.asax è questo codice? –

+0

@LuisTellez il codice si trova all'interno di Application_Error –

+1

Basta sottolineare l'ovvio: hai affermato che 'Response.Redirect' non funziona .. eppure usi' Server.Transfer'. Inoltre, hai provato un URL diretto invece? (senza la tilde) –

risposta

25

Sostituire Response.Redirect("~/Error.aspx"); con:

// 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"); 
+0

Grazie mille, ha funzionato come un fascino !! –

+0

Grazie per la soluzione e le spiegazioni. – nunzabar

+3

hey ho usato lo stesso codice sopra ma ancora dà l'errore La risposta non è disponibile in questo contesto. – Rhushikesh

Problemi correlati