ASP.NET RedirectPermanent Method using C# and VB.NET

ASP.NET 4.0 now includes a feature that developers can easily use in their search engine optimization efforts. This is called the RedirectPermanent method. This method returns a clean 301 redirect from the origin page to the new target page. In the old days of ASP.NET, doing 301 redirection was coded manually, and in some instances required access to the Microsoft IIS administration console. With the release of ASP.NET 4.0, 301 redirections can be easily implemented with a RedirectPermanent method.

301 redirection is essential for the best user experience. If something on your website has been changed or moved to a new permanent location, users will need to be permanently redirected. In addition, search engines can follow this type of redirection, and this redirected-to page will now be the one to rank in Google or other search engines, replacing the old page.

There are different ways to implement the RedirectPermanent method. This tutorial will illustrate these common techniques with sample VB.NET or C# code.

Creating the Sample ASP.NET 4.0 Website

RedirectPermanent is new in ASP.NET 4.0, so you need to create a website using  the .NET 4.0 framework. Refer to the steps below.

1. Launch Visual Web Developer Express 2010.

2. Go to File –> New Website.

3. Select Visual Basic first (equivalent C# code will be provided at the end of this tutorial) and then select “ASP.NET Empty Website.”

4. Indicate the web location to be “File System.” And indicate the project path and name, e.g.

L:aspdotnetprojectsdotnetversion4RedirectPermanent

Click OK.

5. Go to Website –> Add New Item.

6. Select Visual Basic and Web Form from the Templates.

7. Set the file name to originpage.aspx. Make sure “Place code in separate file” has been checked.

8. Click “Add.”

9. Let’s add another ASP.NET page. Go to Website –> Add New Item. Also select Visual Basic and Web form.

10. Set the name to targetpage.aspx and check “Place code in separate file.”

11. Double click originpage.aspx under Solution Explorer. In originpage.aspx, insert the text below between <div>…</div> tags in the source code:

THIS IS THE ORIGIN PAGE.

12. Double click targetpage.aspx and also insert the text below between the <div>…</div> tags.

THIS IS THE TARGET PAGE.

13. Go to File –> Save all.

14. Go to the source code for originpage.aspx and then go to File –> View in Browser. You will see the text indicating this is the origin page. Get the URL in the address bar, for example:

http://localhost:50569/RedirectPermanent/originpage.aspx
 
The path is actually /RedirectPermanent/originpage.aspx for the origin page.

15. Now close this page and go to targetpage.aspx. View this page in the browser. You will see the text indicating this is the target page. Get the URL as well, example:

http://localhost:50569/RedirectPermanent/targetpage.aspx

Implementing RedirectPermanent in Code Behind

The simplest and most direct way to implement RedirectPermanent is in the code behind. If you have an ASP.NET page, say Default.aspx, the code behind page is Default.aspx.vb for VB.NET or Default.aspx.cs for C#. In the above example website, you have created two ASP.NET pages (originpage.aspx and targetpage.aspx) using Visual Basic in the code behind. What if you would like to 301 redirect originpage.aspx to target.aspx? What will happen is that, if originpage.aspx is requested by the server and loaded, the code-behind will execute the RedirectPermanent in the code behind. When the RedirectPermanent method is invoked, it will 301 redirect originpage.aspx to targetpage.aspx. Refer to the steps below for how to do this.

1. Go to originpage.aspx in Design view using VWD Express 2010. This is how it looks in Design View:

2. Double click the empty white region below “THIS IS THE ORIGIN PAGE” so that you can access the page load event for the page. You will then see the VB.NET code behind with the page load event handler:

Partial Class originpage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    End Sub
End Class

3. As you can see, the page load event handler is still empty and does not contain any instructions (between the Protected Sub and End Sub). Let’s instruct the origin page to 301 redirect to the target page:

Response.RedirectPermanent("/RedirectPermanent/targetpage.aspx", True)

The complete Default.aspx.vb code that includes the RedirectPermanent is shown below:

Partial Class originpage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Response.RedirectPermanent("/RedirectPermanent/targetpage.aspx", True)
    End Sub
End Class

4. Go to File –> Save All.

5. To test that the redirection is working, go to originpage.aspx’s source code and then go to File –> View in Browser. You will notice that, upon loading the originpage.aspx page in the browser, it is now 301 redirected to targetpage.aspx.

6. If you have a Firebug (http://getfirebug.com/) installed in your Firefox browser, you can confirm that the return status is 301. Open Firebug, and then go to the NET tab. Load originpage.aspx in the browser by pasting the URL in the browser address bar, and then press enter. This is how it looks in the Net console:

{mospagebreak title=Implementing RedirectPermanent in Global.asax}

Aside from using RedirectPermanent in the code-behind, you can also use Global.asax file for the 301 redirections. Follow the steps below to do this.

1. Delete:

Response.RedirectPermanent("/RedirectPermanent/targetpage.aspx"

from originpage.aspx. This time, you will be using global.asax to 301 redirect originpage.aspx to targetpage.aspx, and not doing it from the code behind.

2. Go to Visual Web Developer Express 2010, and then go to Website –> Add New Item. Select Visual Basic as the programming language (you can also use C#). In the Installed Templates, select “Global Application Class.” Click “Add.” This will add the global.asax file to your projects (if the existing project does not have global.asax).

3. To redirect originpage.aspx to targetpage.aspx, add the Application_BeginRequest subroutine to global.asax:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

If Request.FilePath = "/RedirectPermanent/originpage.aspx" Then
Response.RedirectPermanent("/RedirectPermanent/targetpage.aspx", True)

End If

End Sub

The code above that is added in global.asax will first check to see if the requested ASP.NET is originpage.aspx (using Request.FilePath). If the condition is TRUE, it will then 301 redirect to targetpage.aspx using the RedirectPermanent method.

This is the screen shot of the global.asax:

Summary Equivalent Codes of RedirectPermanent Method

Using VB Code-Behind:

Partial Class originpage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    End Sub
End Class

Equivalent C#:

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class originpage : System.Web.UI.Page
{

 protected void Page_Load(object sender, System.EventArgs e)
 {
  Response.RedirectPermanent("/RedirectPermanent/targetpage.aspx", true);
 }
 public originpage()
 {
  Load += Page_Load;
 }
}

Global.Asax in VB.NET using RedirectPermanent:

<%@ Application Language="VB" %>

<script runat="server">

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

If Request.FilePath = "/RedirectPermanent/originpage.aspx" Then
            Response.RedirectPermanent("/RedirectPermanent/targetpage.aspx", True)

End If
End Sub
   
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
‘Code that runs on application shutdown

End Sub
       
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

‘Code that runs when an unhandled error occurs
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
‘Code that runs when a new session is started
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
‘Code that runs when a session ends.
‘Note: The Session_End event is raised only when the sessionstate mode
‘is set to InProc in the Web.config file. If session mode is set to StateServer
‘or SQLServer, the event is not raised.
End Sub
      
</script>

Equivalent Global.asax in C# using RedirectPermanent:

<%@ Application Language="C#" %>

<script runat="server">
public void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.FilePath == "/RedirectPermanent/originpage.aspx")
{
            Response.RedirectPermanent("/RedirectPermanent/targetpage.aspx", true);

}
}

public void Application_End(object sender, EventArgs e)
{
 //Code that runs on application shutdown

}


public void Application_Error(object sender, EventArgs e)
{
//Code that runs when an unhandled error occurs
}

public void Session_Start(object sender, EventArgs e)
{
//Code that runs when a new session is started
}

public void Session_End(object sender, EventArgs e)
{
//Code that runs when a session ends.
//Note: The Session_End event is raised only when the sessionstate mode
//is set to InProc in the Web.config file. If session mode is set to StateServer
//or SQLServer, the event is not raised.
}
     
</script>