ASP.NET RedirectPermanent

ASP.NET web programming framework

Your ASP.NET program has received a request for a path that you want to redirect permanently to another location. You want search engines such as Google and Bing to change their indexes to point to the new page directly. The new RedirectPermanent method can solve this problem.

Example

Note

First, to call RedirectPermanent you will need to acquire the Response object from the HttpContext. Then, you can call RedirectPermanent: if you pass false as the second parameter, you can perform further actions and avoid an exception.

This C# example uses the RedirectPermanent method in ASP.NET.

Method that calls RedirectPermanent [C#; ASP.NET 4.0]

using System;
using System.Web.UI;

namespace WebApplication6
{
    public partial class List : Page
    {
	protected void Page_Load(object sender, EventArgs e)
	{
	    // Get response.
	    var response = base.Context.Response;
	    // Redirect permanently.
	    response.RedirectPermanent("http://www.dotnetperls.com/list", false);
	}
    }
}

Example output when page visited [HTTP]

HTTP/1.1 301 Moved Permanently
Date: Fri, 13 Aug 2010 16:10:05 GMT
Server: Microsoft-IIS/6.0
Location: http://www.dotnetperls.com/list
Content-Type: text/html; charset=utf-8
Content-Length: 144

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://www.dotnetperls.com/list">here</a>.</h2>
</body></html>

Example output. So what does the RedirectPermanent end up sending to the browser? It sends a HTTP header with the Location key, and an HTML page with a clickable link on it. In the browsers, the HTML will never be seen; instead, the browser will immediately show the URL referenced in the Location header. The HTTP code used is 301, which signifies "Moved Permanently."

Why not just Redirect?

Question and answer

The reason you don't just use Redirect is that search engines will continue to think that the original URL is the one to keep in their index. Because of this, users will experience worse performance because whenever they click on a search engine link, your site will have to handle more requests.

Response Redirect Method

Summary

The C# programming language

The RedirectPermanent is a very useful addiction to the .NET Framework version 4.0. It can save you a lot of coding effort because it is built into the framework. For me, the RedirectPermanent was one of my most anticipated new features in .NET 4.0; it worked perfectly when I changed my software to use it.

ASP.NET Tutorials
.NET