Del via


SPS 2003 : Custom error page for a status 404 error

I was recently working on SPS 2003 issue where we wanted to redirect the user to a custom error page. In MOSS we have an elaborate process to achieve the custom error page functionality.

After quite a bit of research I could find couple of ways but the best and most efficient one seems is to use the HTTP Module for the same. Certain important pointers to remember before using the sample code below. Firstly create your own custom page – preferably a layouts or application page in the layouts directory. Secondly the assembly will have to deployed to the GAC folder and add the Custom HTTP Module entry to the web.config. Lastly do an iisreset to ensure everything is reseted before use of http module.

<Sample code>

using System;
using System.Web;
using System.Net;

namespace Inter
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class Class1 : IHttpModule
    {
        public Class1()
        {
        }

        #region IHttpModule Members

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.EndRequest += new EventHandler(context_EndRequest);
        }

        public void Dispose()
        {
            // TODO:  Add CustomModule.Dispose implementation
        }

        #endregion

        private void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication objHttp = (HttpApplication)sender;
            //objHttp.Context.Response.End();                      
        }

        private void context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication objHttp = (HttpApplication)sender;

            if (objHttp.Context.Response.StatusCode == 404)
            {
                objHttp.Context.Response.Redirect("https://" + objHttp.Context.Request.Url.Authority + "/default.aspx");
            }
        }
    }

}

</Sample Code>

Comments

  • Anonymous
    June 13, 2010
    Thanks a lot, It was very helpful for me.

  • Anonymous
    July 01, 2010
    Hi, Your blog is great. I had used your code in my application but it is working for .aspx pages and when I type a non-existing html page in the site, it is not redirecting to custom error page. Can you please suggest. I had added ".html" extension in my website in IIS and also mapped it to "C:WINDOWSMicrosoft.NETFrameworkv2.0.50727aspnet_isapi.dll" in IIS so that HTTPModule code will be called as HTML page is treated as ASP.net resource but still not working. Please suggest. Thanks and regards.