Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sébastien Richer 194 posts 430 karma points
    Aug 21, 2014 @ 18:09
    Sébastien Richer
    0

    Custom 404 handler works great, except for *.php or *.aspx and etc.

    I'll paste my custom 404 handler after this. Basicallly it's working fine, except that when I send urls like mysite.com/blablabla.php, I get the IIS 404 and not my custom 404. I breakpoint and debug on my custom handler, basically it's never called.

    I feel this has something todo with IIS configuration (running 8.5) but I don't know what exactly.

    If I add <httpErrors existingResponse="PassThrough"/> to my web.config,I instead get an empty response, but still no hit on my handler.

    Thanks for the help!

    using System.Web;
    using umbraco.interfaces;
    using Umbraco.Core;
    using Umbraco.Core.Logging;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    
    namespace BTSPromo.Code
    {
        public class Umbraco404Handler : INotFoundHandler
        {
            private int redirectId;
    
            public bool Execute(string url)
            {
    
                // Use the url to figure out the culture
                var lang = UmbracoContext.Current.ContentCache.GetByRoute(HttpContext.Current.Request.Path.Substring(0, 4));
                var site = UmbracoContext.Current.ContentCache.GetByRoute("/");
    
                if (lang == null)
                {
                    int notFoundNodeId = site.AsDynamic().page404; // Unset content picker, cast to int, will be 0
                    if (notFoundNodeId != 0)
                    {
                        redirectId = notFoundNodeId;
                        return true;
                    }
                    else
                    {
                        // No specified 404
                        return false;
                    }
                }
                else
                {
                    int notFoundNodeId = lang.AsDynamic().page404; // Unset content picker, cast to int, will be 0
                    if (notFoundNodeId != 0)
                    {
                        redirectId = notFoundNodeId;
                        return true;
                    }
                    else
                    {
                        // No specified 404 on language node, try site root
                        notFoundNodeId = site.AsDynamic().page404; // Unset content picker, cast to int, will be 0
                        if (notFoundNodeId != 0)
                        {
                            redirectId = notFoundNodeId;
                            return true;
                        }
                        else
                        {
                            // No specified 404
                            return false;
                        }
                    }
                }
    
                return false;
            }
    
            public bool CacheUrl
            {
                get { return false; }
            }
    
            public int redirectID
            {
                get { return redirectId; }
            }
        }
    }
  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 21, 2014 @ 18:23
    Nicholas Westby
    0

    This thread and the threads it links to may help: http://our.umbraco.org/forum/ourumb-dev-forum/bugs/50332-Custom-404-Only-Works-for-Extensionless-and-ASPX-URLs

    In the end, what worked for me was doing this:

    <httpErrors errorMode="Custom">
        <remove statusCode="404" />
        <error statusCode="404" path="/arbitraryPathThatDoesNotExistAndThatWillRespondWithA404" responseMode="ExecuteURL" />
    </httpErrors>
    

    I don't think I needed to actually set errorMode to "Custom".

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 21, 2014 @ 19:11
    Nicholas Westby
    0

    Also, I should mention that the behavior for my local website (through Visual Studio) was different than the production website. I think I got a blank page for my local website, but I got the correct 404 page for the production website.

Please Sign in or register to post replies

Write your reply to:

Draft