Blog Splash

Permanent Redirect Using ASP.NET

by Kerido Monday, February 22, 2010 7:49 AM

As Google recomments, when changing a website's domain name, you should add the HTTP 301 "Moved Permanently" status to the old address. To my shame, I always thought this is what HttpResponse.Redirect is about. However, using the latter produces the HTTP 302 status which is semantically somewhat different.

A trivial task that ASP.NET has no pretty code for. Here is a convenience class that does the job:

public static class HttpResponseUtility
{
  public static void PermanentRedirect(HttpResponse theResp, string theUrl)
  {
    PermanentRedirect(theResp, theUrl, true);
  }

  public static void PermanentRedirect(HttpResponse theResp,
    string theUrl, bool theEndRequest)
  {
    theResp.Redirect(theUrl, false);
    theResp.StatusCode = 301;  // as opposed to 302 set by Redirect

    if(theEndRequest)
      theResp.End();
  }
}

With this class in place, you simply call the following from a page:

HttpResponseUtility.PermanentRedirect(Response, newUrl);

Also, if you use .NET version 3.5, an extension method would do an even better job (notice the this keyword):

public static class HttpResponseUtility
{
  public static void PermanentRedirect(this HttpResponse theResp, string theUrl)
  {
    PermanentRedirect(theResp, theUrl, true);
  }

  public static void PermanentRedirect(this HttpResponse theResp,
    string theUrl, bool theEndRequest)
  {
    theResp.Redirect(theUrl, false);
    theResp.StatusCode = 301;  // as opposed to 302 set by Redirect

    if(theEndRequest)
      theResp.End();
  }
}

Your client code would then look even prettier:

Response.PermanentRedirect(newUrl);

Monitoring Site Uptime

by Kerido Wednesday, February 10, 2010 8:46 AM

I'd rather not mention the name of my hosting provider, but I'm definitely dissatisfied with their service. In the beginning of February I started monitoring website uptime.

The idea behind the tool I've written is simple. The program runs as a service on a dedicated machine and periodically queries a known resource on the website. The query intervals are adjustable. Currently I've specified them as follows:

  • every 30 minutes if the resource has been successfully accessed;
  • every 5 minutes if the resource cannot be accessed.

This is actually the main reason I decided to write my own tool – you only get hourly monitoring from free services. If you want more frequent pings, you have to pay. And I'm just too old school to pay for this.

If the resource on the website cannot be reached, the tool tries to determine if the server on which it is running, has no connection. To do so, it queries a so called trusted URL. Finally, the site is considered down only if the trusted URL can be reached.

Another concept I put into the tool is analysis. The program automatically displays downtime intervals: start time, end time and duration. Given the sum of these intervals, I calculate total uptime, total downtime, and most importantly, relative uptime. I can perform this kind of analysis for various periods.

After a couple of weeks of monitoring I guess, the hosting provider is somewhat cheating with numbers. They promise 99.9% uptime. My tool shows a more truthful figure.

It makes sense to monitor your website uptime because you need to know what you're paying for. If anybody is interested, I'll probably publish the source code some time later.