Copied to clipboard

Flag this post as spam?

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


  • reense 3 posts 93 karma points
    Jul 25, 2018 @ 13:17
    reense
    0

    ContentFinderByNotFoundHandlers works locally but not on server

    Hi all,

    For handeling the 404 not found pages of my application i created an ApplicationEventHandler. The code is as folllows:

    public class ErrorPageHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            LogHelper.Info<ErrorPageHandler>("application started");
            ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, NotFoundContentFinder>();
    
            base.ApplicationStarting(umbracoApplication, applicationContext);
        }
    
        internal class NotFoundContentFinder : IContentFinder
        {
            public bool TryFindContent(PublishedContentRequest contentRequest)
            {
                LogHelper.Info<ErrorPageHandler>($"has domain {contentRequest.HasDomain}");
    
                if (contentRequest?.HasDomain == true)
                {
                    var currentSiteRootNode = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(contentRequest.UmbracoDomain.RootContentId.Value);
                    LogHelper.Info<ErrorPageHandler>($"currentSiteNode {currentSiteRootNode}");
    
                    if (currentSiteRootNode != null)
                    {
    
    
                        var notFound = currentSiteRootNode.Descendant<NotFound>();
    
    
                        contentRequest.SetResponseStatus(404, "404 Page Not Found");
    
                        contentRequest.PublishedContent = notFound;
                        return contentRequest.PublishedContent != null;
                    }
                }
    
                return false;
            }
        }
    }
    

    This code works locally, and returns my custom NotFound models' publishedContent. However, when i push this to a server, i get the generic 404 page of IIS.

    When I add some debugging strings to the code, I see that this event handler is indeed ran by umbraco on a 404, and that it can find my NotFound model. I can also see that the published content is not null on the server, so the logic of the above handler (should) be fine.

    What could possibly be the reason that I see the standard IIS 404 page? Umbraco can find my published content and can confirm that it is not equal to null.

    Any help is appreciated.

  • Alex Brown 129 posts 618 karma points
    Jul 25, 2018 @ 14:13
    Alex Brown
    0

    Try the following:

    1. Go to /config/umbracoSettings.config
    2. "trySkipIisCustomErrors" to true

    Re-deploy and see if it works.

  • reense 3 posts 93 karma points
    Jul 25, 2018 @ 14:14
    reense
    0

    Alex,

    Thank you for your reply! However, I already tried that..

    Any other suggestions ? :)

  • Alex Brown 129 posts 618 karma points
    Jul 25, 2018 @ 14:17
    Alex Brown
    0

    This sounds dumb but I usually remove the value from the error404 tag in umbracoSettings.config, although I have no proof it actually works. I suppose you could try this?

    Edit: I wrote a dodgy blog on this. What you've done is almost identical to what I do which almost always works for me. https://abrowndev.co.uk/blog/creating-a-custom-404-page/

  • reense 3 posts 93 karma points
    Jul 27, 2018 @ 08:04
    reense
    100

    Alex,

    I fixed it! I added the following code web.config:

    <httpErrors existingResponse="PassThrough" />
    

    And it works now. It was a configuration fault. My bad!

    Anyway, thanks for your help!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies