# Response.Redirect and Response.End

I discovered once myself automatically writing some redirects on ASP.NET pages and I started thinking about API behind these redirects and code readability. Here is what I came out with.

Directs were like this:

Response.Redirect("otherpage.aspx"); Response.End();

Well, what’s the point writing code the way shown above if I can write same thing two ways as follows:

Response.Redirect("otherpage.aspx");Response.Redirect("otherpage.aspx", true);

My point is simple. ASP.NET allows processing continue also after headers are sent to browser. This way we can send user to another page and do some more actions on page. Okay, I want to write about my habit, not about Response.Redirect functional side You can find more about Response.Redirect from C6 Software article Response.Redirect(url) ThreadAbortException Solution.

What is the point of using two lines given in first code example instead of one of those that are shown in second example?

At first, if some other coder reads my code then he or she can see without further thinking and guessing that this is the point where response ends. So there is not much chances that somebody writes code after Response.End and wonders later why this code is never executed.

The second reason why I prefer first style is that the second one has boolean argument. It can have only values true and false and it is not easy to read about what depends on these true and false values. If there was enumerator it was much better because we can use enumerators to give understandable values to different constants we can use in same place.

Of course, now we can use enumerator because we have extension methods. Using extension methods it is possible to write new Response.Redirect method. Something like this:

public enum ResponseEndEnum{    EndResponse,    ContinueResponse} public static class HttpResponseExtensions{    public static void Redirect(this HttpResponse response, string url,                                 ResponseEndEnum responseEnd)    {        bool end = (responseEnd == ResponseEndEnum.EndResponse);        response.Redirect(url, end);    }}

Now if we need to redirect user to another page we can write redirect this way:

Response.Redirect("otherpage.aspx", ResponseEndEnum.EndResponse);

As you can see it all about readibility of the code. Or using another words – saving the time of other people who work with me or who will update my code later.

### 4 thoughts on “Response.Redirect and Response.End”

• schnieds says:

This will never work….

Response.Redirect(“otherpage.aspx”);

Response.End();

Response.End(); is never going to get called because you have already redirected the response to another page. Create this statement, set a breakpoint on the Response.End(); and watch it never get called.

The second approach you mentioned using the ResponseEndEnum will work fine, but your argument is moot as your first approach doesn’t work.

• Gunnar says:

First approach works because redirection takes place. Just Response.End is not run. It is there like “Game Over” when you get killed in action game. It is not part of game, there is no action anymore and it is purely informative. It sais to reader that there happens nothing after redirect – it is the point where response is over.