7

Is it necessary to call Response.End() after Response.Redirect(url)

Update

Thanks for all the answers. Because some answers say that it's necessary and others say no, I have searched more and have found in msdn under remarks the following:

Redirect calls End which raises a ThreadAbortException exception upon completion.

HCL
  • 36,053
  • 27
  • 163
  • 213
  • @m.edmondson: Yes and I give you +1 and the accept :) I have not seen your answer before I have found the answer myself on msdn. That's why I searched newly and wrote my update. – HCL Jan 13 '11 at 16:52
  • 1
    See related [response-end-considered-harmful](http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful) – Michael Freidgeim Jun 01 '13 at 02:45

5 Answers5

11

Response.Redirect calls Response.End for you

I don't agree its good practice - it leads to misleading code.

MSDN:

Redirect calls End which raises a ThreadAbortException exception upon completion.

The laws of HTTP explain that once a response is sent the server is done (no more code gets called)

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
4

Response.Redirect allows you to call Response.End.

Response.Redirect(url, true);
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
2

It's not necessary to call Response.End, in fact if you think that you need to end the response use the overload of Response.Redirect(url, endResponse)

Scott Rickman
  • 560
  • 5
  • 10
0

Not if you pass true as the second parameter: http://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.80).aspx

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
-1

Necessary or not it's still a good practice to call Response.End() at the end of the response. It certainly won't hurt anything either.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • It will hurt because its code clutter - nothing will get called after Response.Redirect() so why have it in there at all? – m.edmondson Jan 13 '11 at 16:56
  • 1
    Actually it does hurt things. Response.End works by throwing a ThreadAbortException. This can have all kinds of nasty side effects in your application. One that I've hit is that a TransactionScope transaction was not cleared from thread local storage, so the next time that thread tried to use a TS it bombed. And would keep bombing until the apppool got recycled. End is there for classic asp to asp.net migrations, but its not something you should be using. Its unfortunate that MS took until .Net 4 to really document this and add Response.CompleteRequest. – Andy May 05 '11 at 17:18
  • See: http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx and http://msdn.microsoft.com/en-us/library/system.web.httpresponse.end.aspx – Andy May 05 '11 at 17:20