another technical blog...technically

Thursday, August 6, 2015

SharePoint 2013 'Page not found (404)' with catalog item page and standard page

If you read these blog post:
SharePoint 2010 ‘Page not found (404)’ page the way it should be and this Inconvenient Catalog Item Page and ‘Page not found’ (404) experience from the master Waldek Mastykarz , and you're here, you're one step closer to the solution.
This blog post extends the Mastykartz strategy with just few lines of code.
 Let's assume you developed those components:
  1. PageNotFoundHttpModule from SharePoint 2010 ‘Page not found (404)’ page the way it should be
  2. PageNotFoundWebPart from SharePoint 2010 ‘Page not found (404)’ page the way it should be 
  3. Something like MaventionCatalogItemReuseWebPart from Inconvenient Catalog Item Page and ‘Page not found’ (404) experience
PageNotFoundHttpModule and PageNotFoundWebPart works also for SharePoint 2013.
MaventionCatalogItemReuseWebPart simply redirects to site collection page not found url, but, since this is a server side redirection, the HTTP status code is 302 and the redirect location it's also well known, so i just modified PageNotFoundHttpModule in this way.
public class PageNotFoundHttpModule : IHttpModule
    {
        #region Const
        const string URL_RELATIVE_PAGENOTFOUND = "/Pages/PageNotFoundError.aspx";
        #endregion

        #region Fields
        private HttpApplication webApplication;
        #endregion

        #region Public methods        
        public void Init(HttpApplication context)
        {
            webApplication = context;
            webApplication.PreSendRequestContent += new EventHandler(webApplication_PreSendRequestContent);
        }

        public void Dispose()
        {
        }
        #endregion

        #region Event handlers
        void webApplication_PreSendRequestContent(object sender, EventArgs e)
        {
            HttpResponse response = webApplication.Response;
            HttpRequest request = webApplication.Request;

            if ((response.StatusCode == 404 && string.Compare(request.Url.AbsolutePath, URL_RELATIVE_PAGENOTFOUND, StringComparison.InvariantCultureIgnoreCase) != 0) ||
               (response.StatusCode == 302 && string.Compare(response.RedirectLocation, URL_RELATIVE_PAGENOTFOUND, StringComparison.InvariantCultureIgnoreCase) == 0))
            {
                webApplication.Server.TransferRequest(URL_RELATIVE_PAGENOTFOUND);
            }
        }
        #endregion
So what i added is essentially a new or condition.
Share:

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?