Anatoly Lubarsky

Logo
MSSQL, .NET, Design. Life and Music

ASP.NET: HttpNotFoundHandler Custom Implementation

Below is a small code snippet, that implements System.Web.HttpNotFoundHandler functionality. Speaking of which - System.Web.HttpNotFoundHandler intercepts 404 (Page not found) request having .aspx extension. Custom implementation used instead because:


  • System.Web.HttpNotFoundHandler is not accessible.
  • Original ProcessRequest throws exception, while I want just to complete request with 404 status.

When to use and why do I need it ? Well - I find it useful when need to "remove" some page or resource, quickly put it offline, but without changing code or moving files - just add a path to web.config and associate with HttpNotFoundHandler. Various scenarios come in mind...


public class HttpNotFoundHandler : System.Web.IHttpHandler
{
    public HttpNotFoundHandler() { }
    
    public void ProcessRequest(HttpContext context)
    {
        string msg404 = "404 - Page not found.";
        context.Response.Write(msg404);
        context.Response.StatusCode = 404;
        context.Response.StatusDescription = msg404;
        context.ApplicationInstance.CompleteRequest();
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

Enjoy


Related Posts:

Saturday, January 13, 2007 10:44 AM

If your feedback doesn't appear right away, please be patient as it may take a few minutes to publish.

Post a Comment

Protected by CAPTCHAEnter the code you see
Name (*)  
E-mail (*)  
Url
Remember

Comment (*)