0

In my requirement i am returning custom error. in global.asax application error is not redirecting any other URL. Response.Redirect(URL),Server.Transfer(URL) is not redirecting. it show Cannot redirect after HTTP headers have been sent. i tried but not working. please try to help me.below is my code

try
        {
            Exception exc = Server.GetLastError();
            ErrorLogger LogError = new ErrorLogger();

            // Handle HTTP errors
            if (exc.GetType() == typeof(HttpException))
            {
                ex = (HttpException)Server.GetLastError();
                int httpCode = ex.GetHttpCode();

                // Filter for Error Codes and set text
                if (httpCode >= 400 && httpCode < 500)
                    ex = new HttpException
                          (httpCode, "Safe message for " + httpCode + " HTTP codes.", ex);
                else if (httpCode > 499)
                    ex = new HttpException
                         (ex.ErrorCode, "Safe message for " + httpCode + " HTTP codes.", ex);
                else
                    ex = new HttpException
                        (httpCode, "Safe message for unexpected HTTP codes.", ex);

                // Log the exception and notify system operators
                ErrorLogger.Error(ex);
                Server.ClearError();
                Response.Clear();
                Response.Buffer = true;
                Response.Redirect("http://www.stackoverflow.com");
                if (!Response.IsRequestBeingRedirected)
                    // Will not be called
                    Response.Redirect("http://www.google.com");

                //ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('ErrorPage.aspx');", true);
                //Response.Write("<script> window.open('http://localhost:54749/ErrorPage.aspx','_blank'); </script>");
                //Response.Redirect("http://www.google.com", true);
                //Response.Redirect("~/ErrorPage.aspx");
            }
            else
            {
                ErrorLogger.Error(exc);
                // LogError.NotifySystemOps(exc);
                // Clear the error from the server
                //Server.ClearError();
            }
        }
        catch (Exception ex)
        {
            ErrorLogger.Error(ex);
        }
SSD
  • 1,373
  • 2
  • 13
  • 20
chaitanay
  • 25
  • 9
  • 1
    Possible duplicate of [Why do I get "Cannot redirect after HTTP headers have been sent" when I call Response.Redirect()?](https://stackoverflow.com/questions/159523/why-do-i-get-cannot-redirect-after-http-headers-have-been-sent-when-i-call-res) – Peter B Jan 09 '18 at 09:39
  • Is this MVC (as from your question tags)? If yes, try Context.Response.Redirect() – SSD Jan 09 '18 at 10:01
  • i already tried HttpContext.Current.Response.Redirect(URL). @Surjit SD – chaitanay Jan 09 '18 at 10:04

3 Answers3

0

According to the MSDN documentation for Response.Redirect(string url), it will throw an HttpException when "a redirection is attempted after the HTTP headers have been sent". Since Response.Redirect(string url) uses the Http "Location" response header (http://en.wikipedia.org/wiki/HTTP_headers#Responses), calling it will cause the headers to be sent to the client. This means that if you call it a second time, or if you call it after you've caused the headers to be sent in some other way, you'll get the HttpException.

One way to guard against calling Response.Redirect() multiple times is to check the Response.IsRequestBeingRedirected property (bool) before calling it.

// Causes headers to be sent to the client (Http "Location" response header)

Response.Redirect("http://www.stackoverflow.com");

if (!Response.IsRequestBeingRedirected)
    // Will not be called
    Response.Redirect("http://www.google.com");

Note* You cannot change the HTTP Response Status Code once a redirect 301/302 has been issued as well.

Barr J
  • 10,636
  • 1
  • 28
  • 46
0

Have you tried Response.Redirect("http://www.google.com", true);

can you tell us also which part of global.asax file you are using this code.

void Application_BeginRequest(object sender, EventArgs e)
    {
}

or it is somewhere else

also try following code

HttpContext.Current.Response.Redirect("http://www.google.com/");
Learning
  • 19,469
  • 39
  • 180
  • 373
  • i already tried your answers. and my code is in this method. protected void Application_Error(Object sender, EventArgs e) { } – chaitanay Jan 09 '18 at 09:59
-1

Try this:

this.Response.Redirect("http://www.stackoverflow.com");
iBug
  • 35,554
  • 7
  • 89
  • 134
Mittal Patel
  • 2,732
  • 14
  • 23