Returning status 500 from an HttpHandler

Recently, on The Code Project, a question was posted about returning the status 500 and it not actually working. I suggested that an HttpHandler would be the way to cope with this, and the original poster asked how to do this. Rather than posting the answer there, I decided to post it here. Without further ado, this is how it is done:

/// <summary> 
/// Base class for ensuring that the handler always returns 
/// a status code. 
/// </summary> 
public abstract class StatusHandlerBase : IHttpHandler 
{ 
  private int _returnStatus = 400; 
  /// <summary> 
  /// Initializes a new instance of <see cref="StatusHandlerBase" />. 
  /// </summary> 
  public StatusHandlerBase() {} 
  /// <summary> 
  /// Initializes a new instance of <see cref="StatusHandlerBase" />. 
  /// </summary> 
  /// <param ref="status">The Http status code</param> 
  public StatusHandlerBase(int status) 
  { 
    _returnStatus = status; 
  } 
  /// <summary> 
  /// Don't let the response be cached by the browser. Set up the status code 
  /// and return. 
  /// </summary> 
  public virtual void ProcessRequest(HttpContext context) 
  { 
    context.Response.Cache.SetCacheablity(HttpCacheability.NoCache); 
    context.Response.Cache.SetNoStore(); 
    context.Response.Cache.SetExpires(DateTime.MinValue);     ParseStatusCode(context, _returnStatus); 
  }   /// <summary> 
  /// Actually set up the status code at this point, and return. 
  /// </summary> 
  protected virtual void ParseStatusCode(HttpContext context, int statusCode) 
  { 
    context.Response.StatusCode = statusCode; 
    context.Response.End(); 
  }   public bool IsReusable 
  { 
    get { return true; } 
  } 
} /// <summary> 
/// This concrete implementation of the <see cref="StatusHandlerBase" /> class 
/// sets up the http handler to return a status code of 500. 
/// </summary> 
public class Return500 : StatusHandlerBase 
{ 
  public Return500() : base(500) {} 
}

Now, one of the things I always like to do is to look for ways to abstract so even in a relatively trivial example like this, there’s abstraction. I’m sorry, but there you go – personality quirk and all of that. There you go though, an HttpHandler that returns a 500 status.

Expression Web – Part 1

Well, I’ve just installed Microsoft Expression Web and started to play around with it. I must admit that what I’ve seen so far impresses me. It has a certain reminiscence of Dreamweaver with a hint of CoffeeCub about it. Mind you, the typical Microsoft Gloss is dripping all over it.

Start it up and select New > Web Site then choose a template and you are good to go. The templates are OK, but you are probably going to want to go beyond them quickly. Still, at least they do produce XHTML and CSS code, which is a major improvement on Microsoft’s earlier web efforts FrontPage and Interdev. Now for the good news, there doesn’t seem to be a FrontPage extension anywhere in site.

As I continue to explore the app, I’ll give you an update as to how well it fits into the web development workflow – and whether or not it gets in the way.