Copied to clipboard

Flag this post as spam?

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


  • karen 186 posts 461 karma points
    Jan 28, 2015 @ 21:50
    karen
    0

    404 handler using IContentFinder not working ?

    Using Umbraco v6.2.1

    Trying to write a 404 handler, following this guide and this thread, but it is not working correctly. I am not sure I am registering the event correctly?

    When I have the following:

    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
        ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, NotFoundContentFinder>();
        base.ApplicationStarting(umbracoApplication, applicationContext);
    }      

    then in the handler (code below) contentRequest.Is404 ALWAYS = false, even though the pages don't exist. When I try searching for example asdf.aspx, the contentRequest uri will show only /asdf (not the .aspx part). I end up seeing the default 404 page (set in umbracoSettings.config)

    When I change it to:

    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
        ContentLastChanceFinderResolver.Current.SetFinder(new NotFoundContentFinder());
        base.ApplicationStarting(umbracoApplication, applicationContext);
    }

    then the handler never gets fired at all. You see the default 404 page (from umbracoSettings.config)

    And finally, when I have this:

    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
        ContentFinderResolver.Current.RemoveType<ContentFinderByNotFoundHandlers>();
        ContentLastChanceFinderResolver.Current.SetFinder(new NotFoundContentFinder());
        base.ApplicationStarting(umbracoApplication, applicationContext);
    }

    The handler gets fired, contentRequest.Is404 returns true, but I end up with the ugly 404. I would like it to fall through and return the default 404 page (since the handler isn't doing anything yet)

    There is not much code in the handler yet, I am just trying to get it to fire and work correctly

    using Umbraco.Core;
    using Umbraco.Core.Services;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    using umbraco;
    using System.Linq;
    
    namespace WHRX.Helpers {
        public class NotFoundContentFinder : IContentFinder {
    
            public bool TryFindContent(PublishedContentRequest contentRequest) {
    
                if (contentRequest.Is404) {
    
                    var rootNode = uQuery.GetRootNode();
                    if (rootNode != null) {
                        // do something
                    }
                }
                return contentRequest.PublishedContent != null;
            }
        }
    }
  • karen 186 posts 461 karma points
    Jan 30, 2015 @ 18:01
    karen
    1

    ok this is what I ended up doing that seems to work (adds 404 handler, defaults to default 404 page in config file if no other solution found).  I added code to look up and display the default 404 page.

    add the event handler (3rd version from above)

    protectedoverridevoidApplicationStarting(UmbracoApplicationBase umbracoApplication,ApplicationContext applicationContext){
       
    ContentFinderResolver.Current.RemoveType<ContentFinderByNotFoundHandlers>();
       
    ContentLastChanceFinderResolver.Current.SetFinder(newNotFoundContentFinder());
       
    base.ApplicationStarting(umbracoApplication, applicationContext);
    }

     

    and the handler:

           public bool TryFindContent(PublishedContentRequest contentRequest) {
    
                Umbraco.Core.Models.IPublishedContent foundContent = null;
    
                if (contentRequest.Is404) {
    
                    // add special code here to look up how to handle the 404
    
                    // if nothing found, try to find default error page
                    if (foundContent == null) {
    
                        // attempt to get default Error page
                        var defaultErrorPageList = umbraco.UmbracoSettings._umbracoSettings.SelectNodes("/settings/content/errors/error404");
                        if (defaultErrorPageList.Count > 0) {
                            int i404PageId = 0;
                            int.TryParse(defaultErrorPageList.Item(0).InnerText, out i404PageId);
                            foundContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(i404PageId);
                        }
    
                        // if you want, fall back to a hard coded number
                        foundContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(1062);
                    }
    
                }
                if (foundContent != null) contentRequest.PublishedContent = foundContent;
    
                return contentRequest.PublishedContent != foundContent;
            }

     

  • shinsuke nakayama 109 posts 250 karma points
    Feb 21, 2015 @ 15:13
    shinsuke nakayama
    1

    Hi Karen,

    Thank you for your post, this helped me alot in Umbraco 7.1.9

    I thought I'll share what i did as well. My code is based on your code above and the link you've given above

    http://creativewebspecialist.co.uk/2013/08/07/the-new-way-to-do-a-404-umbraco-handler/

    What I've done is i searched for the level 1 tree node, then search for the 2nd level tree where the document type is equal to "Templateerrorpage". In this scenario, every website must have 404 page in level 2.

    public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            //Check request is a 404
            if (contentRequest.Is404)
            {
                //Get the home node
                var home = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute("/");
    
                //Get the 404 node
                var notFoundNode = home.Children.Single(x => x.DocumentTypeAlias == "Template_error_page");
    
                //Set Response Status to be HTTP 404
                contentRequest.SetResponseStatus(404, "404 Page Not Found");
    
                //Set the node to be the not found node
                contentRequest.PublishedContent = notFoundNode;
            }
    
            return contentRequest.PublishedContent != null;
        }
    

    Cheers

    Shinsuke

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 21, 2015 @ 15:40
    Dennis Aaen
    0

    Hi Shinsuke,

    Perhaps you will also find this information useful http://24days.in/umbraco/2014/urlprovider-and-contentfinder/. It´s a blogpst written by Jeroen Breuer from the last year Umbraco advent calendar.

    Hope this helps,

    /Dennis

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Nov 12, 2016 @ 01:11
    Nicholas Westby
    0

    For anybody else that comes across this...

    I had a similar issue on Umbraco 7.5.4. The problem in my case was that I didn't have a template set for the 404 page, so even though it was found, it could not be rendered.

  • Jay 425 posts 652 karma points
    May 04, 2017 @ 10:43
    Jay
    0

    Anyone had any issues with url that comes with special characters like

    /fr-ca/à-propos-de/fdsafdsf

    The Custome 404 Handler is not picking up with special characters in the url. Any idea?

  • Jay 425 posts 652 karma points
    May 04, 2017 @ 13:28
    Jay
    0

    The normal IIS error, i found out why.

    within the HttpError tag in web.config, i need the existingResponse="PassThrough"

Please Sign in or register to post replies

Write your reply to:

Draft