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
    Apr 18, 2014 @ 21:32
    Sébastien Richer
    0

    404 handler works great locally, not online

    I've got this nice 404 handler that works fine on my local machine. When I deploy to my live server, it does not work. I looked everywhere, those 2 websites (live and local) are almost identical (other than connection string and stuff). (To be super sure, I too a .bak of my live DB and successfully ran with that locally.)

    The code runs on 2 files.

    Umbraco404HandlerHook.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Web.Routing;
    
    namespace MyNamespace.Code
    {
        public class Umbraco404HandlerHook : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentLastChanceFinderResolver.Current.SetFinder(new Umbraco404Handler());
                base.ApplicationStarting(umbracoApplication, applicationContext);
            }
        }
    }

    And Umbraco404Handler.cs

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    
    // Needs interfaces and cms references
    
    namespace MyNamespace.Code
    {
        public class Umbraco404Handler : IContentFinder
        {
    
            public bool TryFindContent(PublishedContentRequest contentRequest)
            {
    
                Utilities.LogText("Umbraco404Handler - 1");
    
                // Safety check, this should never occur
                if (contentRequest != null)
                {
    
                    Utilities.LogText("Umbraco404Handler - 2");
    
                    // Check request is a 404
                    if (contentRequest.Is404)
                    {
    
                        Utilities.LogText("Umbraco404Handler - 3");
    
                        // Need a domain to get the 404 page
                        if (contentRequest.Domain != null)
                        {
    
                            Utilities.LogText("Umbraco404Handler - 4");
    
                            var domainNode = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(contentRequest.Domain.RootNodeId);
                            if (domainNode != null)
                            {
    
                                Utilities.LogText("Umbraco404Handler - 5");
    
                                // Todo: Have this field label come from a config?
                                int notFoundNodeId = domainNode.AsDynamic().page404; // Unset content picker, cast to int, will be 0
                                if (notFoundNodeId != 0)
                                {
    
                                    Utilities.LogText("Umbraco404Handler - 6");
    
                                    // Get the 404 node
                                    var notFoundNode = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(notFoundNodeId);
                                    if (notFoundNode != null)
                                    {
    
                                        Utilities.LogText("Umbraco404Handler - 7 - " + notFoundNode.Name);
    
                                        // Set the node to be the not found node
                                        contentRequest.PublishedContent = notFoundNode;
    
                                    }
                                }
                                else
                                {
                                    // Todo: No specified 404, this could have a fallback in settings?                    
                                }
                            }
    
                        }
                        else 
                        { 
                            // Todo: No domain, this could have a fallback in settings?                    
                        }
    
                    }
    
                }
    
                // Finally, we return weather we have found a node to use for this request
                return contentRequest.PublishedContent != null;
            }
    
        }
    }

     

    I added these logging calls to figure out what was going on on my live site and whenever a 404 is hit, the code traces up to #7 with no problem, but I keep seeing the default IIS 404 page:

    404 - File or directory not found.

    What could be causing this issue, I mean... the code runs, and correctly, but the live site 404 seems to be bypassed by something else...

    Any help will be much appreciated!

     

  • Sébastien Richer 194 posts 430 karma points
    Apr 18, 2014 @ 22:27
    Sébastien Richer
    1

    Fixed! Needed this in my web.config on my live site :

    <system.webServer>
        <httpErrors existingResponse="PassThrough"/>
    </system.webServer> 
Please Sign in or register to post replies

Write your reply to:

Draft