Thursday, August 20, 2009

Creating a custom ASP.Net outage message that returns a 503

If you ever need to bring down a website for maintenance you should consider the impact that may have on search engines' ability to index your site. If your site returns an incorrect status code e.g. 404, then there is a possibility that search engines will de-index your pages.

In this scenario, Google recommends returning a 503 Service Unavailable status to any requests.

In ASP.Net this is simple to achieve - just stop the Application Pool for the site!

However what if you wish to display a user-friendly error page that shows some useful information e.g. contact details.

Most error codes in IIS can be mapped to custom error pages or URLs however a 503 is not one of these. In order to get around this, one method is to return the 503 response via some custom code.

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.StatusCode = 503;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
}
}

This will work well for requests to this particular page but we want this custom error to show for all request to the site. One way to ensure this is to map this page as a custom 404 error. That way any requests to the site will be shown the friendly outage message and the search bots will get their 503. Everybody's happy!

No comments:

Post a Comment