:::: MENU ::::

Thursday, May 22, 2008

I previously talked about the importance of using the correct kind of redirect to optimize your website for search engines in an article titled. World Of HTTP/1.1 Status Codes. I just recently decided to create a C# Utility class to help me in this endeavor and to extended the far from complete HttpResponse.Redirect method. I am using a new C# 3.0 language extension called Extension Methods. Basically what the extension method does is, it allows you to, add methods to types that you don’t have the ability to modify, in my case the HttpResponse class.

I have created the following code to give me better control over my redirects in the HttpResponse class.

  1. public static void Redirect(this HttpResponse response, int type, string url)  
  2. {  
  3.     response.Clear();  
  4.   
  5.     switch (type)  
  6.     {  
  7.         case 301:  
  8.             response.StatusCode = (int)HttpStatusCode.MovedPermanently;  
  9.             response.StatusDescription = "Moved Permanently";  
  10.             break;  
  11.   
  12.         case 302:  
  13.             response.StatusCode = (int)HttpStatusCode.Found;  
  14.             response.StatusDescription = "Found";  
  15.             break;  
  16.   
  17.         case 303:  
  18.             response.StatusCode = (int)HttpStatusCode.SeeOther;  
  19.             response.StatusDescription = "See Other";  
  20.             break;  
  21.   
  22.         case 304:  
  23.             response.StatusCode = (int)HttpStatusCode.NotModified;  
  24.             response.StatusDescription = "Not Modified";  
  25.             break;  
  26.   
  27.         case 307:  
  28.             response.StatusCode = (int)HttpStatusCode.TemporaryRedirect;  
  29.             response.StatusDescription = "Temporary Redirect";  
  30.             break;  
  31.   
  32.         default:  
  33.             goto case 302;  
  34.     }  
  35.   
  36.     response.RedirectLocation = url;  
  37.   
  38.     response.ContentType = "text/html";  
  39.     response.Write("<html><head><title>Object Moved</title></head><body>");  
  40.     response.Write("<h2>Object moved to <a href=\"" + HttpUtility.HtmlAttributeEncode(url) + "\">here</a>.</h2>");  
  41.     response.Write("</body></html>");  
  42.   
  43.     response.End();  
  44. }  
public static void Redirect(this HttpResponse response, int type, string url)
{
 response.Clear();
 
 switch (type)
 {
  case 301:
   response.StatusCode = (int)HttpStatusCode.MovedPermanently;
   response.StatusDescription = "Moved Permanently";
   break;
 
  case 302:
   response.StatusCode = (int)HttpStatusCode.Found;
   response.StatusDescription = "Found";
   break;
 
  case 303:
   response.StatusCode = (int)HttpStatusCode.SeeOther;
   response.StatusDescription = "See Other";
   break;
 
  case 304:
   response.StatusCode = (int)HttpStatusCode.NotModified;
   response.StatusDescription = "Not Modified";
   break;
 
  case 307:
   response.StatusCode = (int)HttpStatusCode.TemporaryRedirect;
   response.StatusDescription = "Temporary Redirect";
   break;
 
  default:
   goto case 302;
 }
 
 response.RedirectLocation = url;
 
 response.ContentType = "text/html";
 response.Write("<html><head><title>Object Moved</title></head><body>");
 response.Write("<h2>Object moved to <a href=\"" + HttpUtility.HtmlAttributeEncode(url) + "\">here</a>.</h2>");
 response.Write("</body></html>");
 
 response.End();
}

So now in your code you don’t have to jump through hoops to chance the StatusCode, StatusDescription, RedirectLocation, and ContentType, just so you can respond with a 301 Redirect instead of a 302 Redirect (the default for HttpResponse.Redirect and the most dangerous of the redirects from an SEO point of view). All that you need to have access to is the Response property from your Page or Context and you are good to go.

Response.Redirect(301, "http://www.coderjournal.com");  

Response.Redirect(301, "http://www.coderjournal.com");

So that is all you need to do to give your self better control over your redirects in .NET. You can also use this same C# 3.0 Extension Methods for any object that you need to add a custom method on to.

More

Categories: ,