0

I don't want the thread abort exception when i use response.redirect method inside try catch block . I give end response is true. Is there any possible way instead of throws thread exception. The page is redirected the login page.

This is my code,

 try
    {
        Response.Redirect("login.aspx?fromLnkPg=1", true);
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message.ToString());
    }
Dave
  • 8,163
  • 11
  • 67
  • 103
  • I want end response property true in response.redirect method. please provide any possible way. – user2739544 Sep 13 '13 at 09:56
  • possible duplicate of [Why Response.Redirect causes System.Threading.ThreadAbortException?](http://stackoverflow.com/questions/2777105/why-response-redirect-causes-system-threading-threadabortexception) – Liam Sep 13 '13 at 09:59
  • that fine. Is there any possible way... I don't want to put two catch block. – user2739544 Sep 13 '13 at 10:05
  • 1
    You want to use Redirect with true as endResponse parameter value but you don't want to have two catch blocks? Then what you are asking is impossible - you need to either use false, two catch blocks or forego Response.Redirect entirely in favor of a different way of redirecting. – S_F Sep 13 '13 at 10:09

3 Answers3

1

As linked here

you need to pass false not true in

Response.Redirect("login.aspx?fromLnkPg=1", false);

followed by

Context.ApplicationInstance.CompleteRequest();
Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190
  • 1
    Out of curiosity, why do you need it to be true? – S_F Sep 13 '13 at 10:06
  • If you pass true the thread will be aborted and it will throw an exception. Apart from the overhead of handling exceptions this appears to suggest you want something to happen after the redirect happens, which is wrong. Do what you need to do, then pass the request to the next page. – Liam Sep 13 '13 at 10:10
  • nice question sf, bcoz i use the fiddler debugger. It displayed the html outputted page. Where the page is redirected. If i give true for end response. The fiddler debugger html header only login.aspx page... The process also stopped. – user2739544 Sep 13 '13 at 10:10
  • That's correct functionality. The redirect does exactly that, redirects **on the server**. There's no reason to write your code so you can use fiddler, fiddler won't be running on the server when you deploy. Your looking at this all wrong. – Liam Sep 13 '13 at 10:13
0

You could do

try
{
    Response.Redirect("login.aspx?fromLnkPg=1", true);
}
catch (Exception ex)
{
    // Removed as not needed due to redirect Response.Write(ex.Message.ToString());
    Response.Redirect("hompage.aspx");
}

You may want to use

Response.IsClientConnected

So

bool b = Response.IsClientConnected;
Response.Redirect("login.aspx?fromLnkPg=1", b);
Dave
  • 8,163
  • 11
  • 67
  • 103
0

try follwing

try
{
    Response.Redirect("login.aspx?fromLnkPg=1", true);
}
catch (ThreadAbortException) 
{
}
catch (Exception ex)
{
    Response.Redirect("home.aspx");        
}
Dhaval
  • 2,801
  • 20
  • 39