12

I was told that Respond.Redirect is an expensive process because it raises a ThreadAbortException. So instead, we should be using the CompleteRequest function instead. So I gave it a try but I noticed the codes below it still runs, which I do not want. I want to instantly force the browser to jump to another website.

Public Shared Sub TestCompleteRequest()
            If 1 = 1 Then
                System.Web.HttpContext.Current.Response.Redirect("Http://Google.com", False)
                System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest()
            End If

            Throw New ApplicationException("Hello, why are you here?")
End Sub

As for the code above, the ApplicationException is still thrown. But why? :(

Lasker
  • 353
  • 1
  • 2
  • 13

2 Answers2

18

One method doesn't replace the other directly. The CompleteRequest() method does not end execution when it's called. So if that's really what you want to do then Response.Redirect(string) would be the way to go.

CompleteRequest() simply bypasses the Response.End() method, which is what generates the ThreadAbortException you mentioned, but crucially CompleteRequest() flushes the response buffer. This means the HTTP 302 redirect response is sent to the browser at the line where you call CompleteRequest(), which gives you a chance to do operations that don't affect the response after it's been sent to the user.

The solution for you really depends on what you need to achieve, can you provide an example of what you're using Response.Redirect for and what other code is in the same method?

Community
  • 1
  • 1
greg84
  • 7,541
  • 3
  • 36
  • 45
  • Hi Greg, thanks for your answer. Here is what the code is suppose to do. If a user come into the page and he is not logged in, I'll like to redirect him to another login page. So the 'IF' statement in the code above should be 'if user is not login, then redirect here' and ignore all the codes below it. – Lasker Jun 29 '12 at 08:40
  • 2
    In that case you would either have to use `Response.Redirect(string)` in the if block, or return from the Sub just after the call to `CompleteRequest()`. I think in VB.NET you can just put `Return` in before the `End If`. – greg84 Jun 29 '12 at 09:25
  • Yeah, I think I'll go with the CompleteRequest option and try not to run any other codes after it. Thanks! :) – Lasker Jun 29 '12 at 10:04
  • `This means the HTTP 302 redirect response is sent to the browser at the line where you call CompleteRequest()` - This doesn't seem to be correct -The page life cycle completes and it only stops the further events at the HTTP pipeline! – BornToCode Aug 11 '16 at 09:21
3

Calling a method in the ASP.NET framework deals with the request, but the fact is you're still writing and running VB.NET - there's nothing in the language (nor should there be, I'd say) that indicates 'when this method returns, perform an Exit Sub'.

Who's to say you wouldn't want to execute some more of the method after telling ASP.NET to complete the request, anyway?

AakashM
  • 62,551
  • 17
  • 151
  • 186