Microsoft Knowledge Base Email Alertz

KBAlertz.com: This article demonstrates how to use the

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 307903 - Last Review: August 28, 2007 - Revision: 3.5

How To Use Response.Redirect in ASP.NET with Visual C# .NET

This article was previously published under Q307903

On This Page

SUMMARY

This article demonstrates how to use the Redirect method of the HttpResponse class in ASP.NET applications to redirect a user to another URL.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000 or Microsoft Windows XP
  • Microsoft .NET Framework
  • Microsoft Internet Information Server (IIS)

Create an ASP.NET Web Application Using Visual C# .NET

The following steps demonstrate how to create a new ASP.NET Web Application project named Redirector.
  1. Open Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, under Project Types, click Visual C# Projects. Under Templates, click ASP.NET Web Application.
  4. In the Location text box, type Redirector. If you are using the local server, you can leave the server name as http://localhost so that the Location text box is set to http://localhost/Redirector.

Description of the HttpResponse.Redirect Method

The HttpResponse class implements two overloaded versions of the Redirect method.
  • The first overloaded method takes only one input parameter, which is the URL for the target location. This version is defined as follows:
    public void Redirect(string url); 
    					
  • The second overloaded method takes two input parameters: the URL for the target location, and a boolean value that indicates whether to stop running the current page. This version is defined as follows:
    public void Redirect(string url, bool endResponse);
    					
When you use the first overloaded version, the second overloaded version is called internally and is passed a boolean value of True for the second input parameter. For more information about the HttpResponse class and its related methods, refer to the REFERENCES section.

Create a Running Sample

This sample demonstrates how to implement the Redirect method in the Page_Load event of a code-behind page. This code implements the first overloaded version that is listed in the previous section.
  1. Follow these steps to add a new Web Form named Redirector_Test.aspx:
    1. In Solution Explorer, right-click the project node, point to Add, and then click Add Web Form.
    2. Name the .aspx page Redirector_Test.aspx, and then click Open.
  2. In the editor, right-click the .aspx page, and then click View Code. This opens the code-behind page in the editor.
  3. Add the following code to the Page_Load event:
    Response.Redirect("http://www.microsoft.com");
    					
  4. On the File menu, click Save All to save the Web Form and other associated project files.
  5. In the Visual Studio .NET Integrated Development Environment (IDE), on the Build menu, click Build to build the project.
  6. In Solution Explorer, right-click the page, and then click View in Browser to run the page. Notice that the page opens in the browser and automatically redirects you to the Microsoft Web site.

Troubleshooting

  • If you try to redirect after the headers are sent to the browser, you receive an HttpException exception. To resolve this problem, use the HttpResponse.BufferOutput property to buffer the output when appropriate. This property is set to True by default.

    For more information about the HttpResponse.BufferOutput property, refer to the REFERENCES section.
  • You may receive a ThreadAbortException exception when you use this method.For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
    312629  (http://kbalertz.com/Feedback.aspx?kbNumber=312629/EN-US/ ) PRB: ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer

REFERENCES

For more information, refer to the following topics in the Microsoft .NET Framework Software Development Kit (SDK) documentation:
HttpResponse Class
http://msdn2.microsoft.com/en-us/library/system.web.httpresponse(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.web.httpresponse(vs.71).aspx)

HttpResponse.Redirect Method
http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.redirect(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.redirect(vs.71).aspx)

HttpResponse.BufferOutput Property
http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.bufferoutput(vs.71).aspx (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpresponseclassbufferoutputtopic.asp)
The ASP.NET Developer Center (http://msdn.microsoft.com/nhp/Default.asp?contentid=28000440) is a good source for articles, headlines, and other information related to ASP.NET.

For tutorials on the .NET Framework and Visual Studio .NET, refer to the following Microsoft Web site:
http://www.codeplex.com (http://www.codeplex.com)
For an introduction to ASP.NET, visit the following Microsoft Web site:
ASP.NET Tutorials
http://samples.gotdotnet.com/quickstart/aspplus/ (http://samples.gotdotnet.com/quickstart/aspplus/)

APPLIES TO
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB307903
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please