HOW TO: Use Response.Redirect in ASP.NET with Visual Basic .NET (312063)
The information in this article applies to:
- Microsoft ASP.NET (included with the .NET Framework 1.1)
- Microsoft ASP.NET (included with the .NET Framework) 1.0
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
This article was previously published under Q312063 For a Microsoft Visual C# .NET version of this article, see 307903.
For additional information about how to perform this task by using Microsoft Active Server Pages, click the article number below
to view the article in the Microsoft Knowledge Base:
159402 HOW TO: Use Response Redirect in a Server Script
IN THIS TASKSUMMARY
This article demonstrates how to use the Redirect method of the HttpResponse class in ASP.NET applications to redirect a user to another URL.
back to the top
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)
back to the top
Create an ASP.NET Web Application Using Visual Basic .NET
The following steps demonstrate how to create a new ASP.NET Web Application project named Redirector.
- Open Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, under Project Types, click Visual Basic Projects. Under Templates, click ASP.NET Web Application.
- 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.
back to the top
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:
Overloads Public Sub Redirect(ByVal url As String)
- 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:
Overloads Public Sub Redirect(ByVal url As String, _
ByVal endResponse As Boolean)
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.
back to the top
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.
- Follow these steps to add a new Web Form named Redirector_Test.aspx:
- In Solution Explorer, right-click the project node, point to Add, and then click Add Web Form.
- Name the .aspx page Redirector_Test.aspx, and then click Open.
- In the editor, right-click the .aspx page, and then click View Code. This opens the code-behind page in the editor.
- Add the following code to the Page_Load event:
Response.Redirect("http://www.microsoft.com")
- On the File menu, click Save All to save the Web Form and other associated project files.
- In the Visual Studio .NET Integrated Development Environment (IDE), on the Build menu, click Build to build the project.
- 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.
back to the top
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 PRB: ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer
back to the top
REFERENCES
For more information, refer to the following topics in the Microsoft .NET Framework Software Development Kit (SDK) documentation:
The ASP.NET Developer Center 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:
For an introduction to ASP.NET, refer to the following section in GotDotNet:
back to the top
Modification Type: | Major | Last Reviewed: | 5/21/2003 |
---|
Keywords: | kbHOWTOmaster KB312063 kbAudDeveloper |
---|
|