Please navigate to the bottom of the page for Table of Contents

Tuesday, May 24, 2011

How to redirect user to another page using ASP.NET

During the process of serving a user’s request for a page data, we may need to redirect (send) him to another page for more processing. For example, if you are trying to access some content on the website that requires you to login first, then you would be redirected to the login page first. Another example is when you want to show a more detailed or richer content to the user but do not want the URL to change.

ASP.NET provides 2 distinct mechanisms for achieving the above 2 scenarios:

  1. HttpResponse.Redirect Method (String, Boolean)
  2. HttpServerUtility.Transfer Method (String)

HttpResponse.Redirect (commonly known as Response.Redirect)

HttpResponse.Redirect method is more commonly known as Response.Redirect(…). The Response object is exposed on aspx pages by Page.Response property which is of type HttpResponse.

public void Redirect(
string url,
bool endResponse
)


As the name implies, this method redirects/tells the user’s browser (client) to navigate to the new URL. You can specify the new URL and whether execution of the current page should terminate or not. Note that you can use both absolute URL and relative URL’s for the url parameter.


Note that ASP.NET performs the redirection by returning a 302 HTTP status code. A 302 response is generally indicative of “Moved Temporarily”. I will show you how to send a 301 response code (Moved Permanently) in a later blog post.


HttpServerUtility.Transfer (commonly known as Server.Transfer)


HttpServerUtility.Transfer method is more commonly known as Server.Transfer(..). The Server object is exposed on aspx pages by Page.Server property which is of type HttpServerUtility. These methods should be used in the case where you actually want to transfer the processing of the current request to another page without updating the user browser.


There are a couple of variations of this call:


public void Transfer(
string path
)

For the current request, terminates execution of the current page and starts execution of a new page by using the specified URL path of the page. The page transferred to should be another .aspx page. For instance, a transfer to an .asp or .asmx page is not valid. The Transfer method preserves the QueryString and Form collections. In addition, Transfer calls End, which throws a ThreadAbortException exception upon completion.

public void Transfer(
string path,
bool preserveForm
)



If you set the preserveForm parameter to true, the target page will be able to access the view state of the previous page by using the PreviousPage property.


To summarize, HttpResponse.Redirect method redirects/tells the user’s browser (client) to navigate to the new URL. In contrast, HttpServerUtility.Transfer should be used in the case where you actually want to transfer the processing of the current request to another page without updating the user browser.

3 comments: