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.

40 comments:

  1. very easy to understand

    ReplyDelete
  2. This is one of the cult game now, a lot of people enjoy playing them . Also you can refer to the game :
    https://digitalnewhunt.in/mobile-number-sim-ditails-location-kaise-pata-kare/

    ReplyDelete
  3. Good article contains necessary information, keep updating us by posting useful articles...
    Best Online Software Training Institute | Asp.Net Training

    ReplyDelete
  4. NICE ARTICLE AND VERY INFORMATIVE I Found this article useful same as your. you can visit by clicking java training in Hyderabad

    ReplyDelete
  5. Nice article
    Unicsol offers Best <"full stack developer course in hyderabad"get trained by 10+years of experienced faculty and get placed as full stack developer.

    ReplyDelete
  6. This is very good content you share on this blog. it's very informative and provide me future related information.
    hadoop admin online training

    ReplyDelete
  7. Pretty article! I found some useful information in your blog....

    so here we provide,

    We provide you with flexible services and complete hybrid network solutions. It can provide your organisation with exceptional data speeds, advanced external security protection, and high-resilience by leveraging the latest SD-WAN and networking technologies to monitor, manage and strengthening your organisation’s existing network devices.

    https://www.quadsel.in/networking/>
    https://twitter.com/quadsel/
    https://www.linkedin.com/company/quadsel-systems-private-limited/
    https://www.facebook.com/quadselsystems/

    #quadsel #network #security #technologies #managedservices #Infrastructure #Networking #OnsiteResources #ServiceDeskSupport #StorageServices #WarrantyAMCServices #datacentersolutions #DataCenterBuild #EWaste #InfraConsolidation #DisasterRecovery #NetworkingServices #ImagingServices #MPS #Consulting #WANOptimisation #enduserservices

    ReplyDelete
  8. I like you post. It is very useful to all.
    In this post having more useful information and improve our knowledge. Can you share more valuable information present Technology.

    Data Analytics Training in Gurgaon
    DATA ANALYTICS WITH R CERTIFICATION in Gurgaon
    Power Bi Training in GUrgaon
    SQL Training in Gurgaon
    Advanced Excel /VBA training in Gurgaon

    ReplyDelete
  9. Excellent Blog! I would like to thank you for the efforts you have made in writing this post. Gained lots of knowledge.

    AWS Training in Hyderabad

    ReplyDelete
  10. The superb highly informative blog I’m about to share this with all my contacts.
    user experience consulting

    ReplyDelete
  11. This post was very informative. This blog is very informative and it's exactly what we need. Keep writing such educational blogs. I look forward to your next posts.
    Angular Course in Pune

    ReplyDelete
  12. Thanks for sharing the programming interview questions and answers such useful information thanks for sharing
    Keep posting more

    ReplyDelete
  13. great post, thanks for sharing this valuable information. Full Stack Course In Pune

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete