Copied to clipboard

Flag this post as spam?

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


  • Tobias Pettersson 19 posts 112 karma points
    Sep 08, 2017 @ 06:38
    Tobias Pettersson
    0

    IContentFinder - TryFindContent contentRequest.Is404 always returns false.

    I'm trying to implement a custom 404 page and I got to work with a little help from this:

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

    What I didn't get to work is the check "contentRequest.Is404" wich always returns false.

    This is what my code looks like now, but i would like contentRequest.Is404 instead of checking if my Error404Page is null:

    public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            var settingsContent = __umbracoHelper.TypedContent(__siteSettings.SettingsPageId);
            var settingsPage = DefaultMapper.Map<SettingsPageContent>(settingsContent);
    
            if (settingsPage.Error404Page != null)
            {
                contentRequest.SetResponseStatus(404, "404 Page not found");
    
                contentRequest.PublishedContent = settingsPage.Error404Page;
            }
    
            return contentRequest.PublishedContent != null;
        }
    

    Anyone who could tell me if I'm doing something wrong or if the Is404

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Sep 08, 2017 @ 09:50
    Damiaan
    0

    If you use ContentLastChanceFinderResolver.Current.SetFinder(), the content finder will be used as a 404 and the page will return a 404 status code by default. No need to set the "ResponseStatus" to 404.

    The contentRequest.Is404 is a property which is used to return a "404 not found" status code by just using the next code in your contentFinder

    contentRequest.SetIs404();
    

    But you don't need it if you use the ContentLastChanceContentFinder.

    When should you use the SetIs404? If you have a contentFinder that the right contentfinder but the content is still not found (e.g. the querystring does not match a certain pattern), you could/should implement a 404 status code.

  • Tobias Pettersson 19 posts 112 karma points
    Sep 08, 2017 @ 11:51
    Tobias Pettersson
    0

    Hi Damiaan,

    thanks for the reply!

    Seems I can't get it to work tho. I get an errormessage like this:

    Resolution is not frozen, it is not yet possible to get values from it.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Source Error:

    Line 51: private int GetPageId(IContentService contentService, string contentTypeName) Line 52: { Line 53: var page = contentService.GetRootContent().FirstOrDefault( Line 54: content => content.ContentType.Alias.Equals(contentTypeName, StringComparison.OrdinalIgnoreCase)); Line 55:

    Seems something is not right in the application startup event, but it looks like the ApplicationStarting is the right event to use for the ContentLastChanceFinderResolver.

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

    Any ideas what I'm missing?

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Sep 08, 2017 @ 12:26
    Damiaan
    0

    Hi

    The source refers to line 51. But from which file?

    ApplicationStarting is the right file indeed. Have you rebuild the project in visual studio? If resolution is frozen, you have probably still an old version of you class trying to set a resolver on the ApplicationStarted event.

    I don't use the base.ApplicationStartingEvent. We call it just like this and it works like a charm:

    namespace xyz {
      public class EventRegistration : ApplicationEventHandler
      {        
         protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
         {                 
            ContentLastChanceFinderResolver.Current.SetFinder(new PageNotFoundContentFinder());
         }
       }
    }
    
  • Tobias Pettersson 19 posts 112 karma points
    Sep 08, 2017 @ 13:10
    Tobias Pettersson
    0

    I found the issue for the :

    Resolution is not frozen, it is not yet possible to get values from it.

    I tried to fetch my settingspage, where I have my 404 page as a ContentPicker with a custom made mapper. So thats not gonna work :)

    Im now fetching it throught the:

    contentRequest.RoutingContext.UmbracoContext.ContentCache.GetAtRoot()
    

    So now the application initializes the right finder, I do however get the old "ugly" 404 page (the one set as default) still even though I see in my code that my custom made 404 page is set to contentRequest.PublishedContent.

    I removed that from the config so it shouldn't even find that page for all I know.

    Really appreciate your help!

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Sep 08, 2017 @ 13:23
    Damiaan
    0

    Verify that:

    1. Have you set the trySkipIisCustomErrors to TRUE in the /config/umbracoSettings.config ?
    2. you have a template on your 404 node in the content.

    kind regards

  • Tobias Pettersson 19 posts 112 karma points
    Sep 11, 2017 @ 06:12
    Tobias Pettersson
    0

    Hi Damiaan,

    i have set the trySkiplisCustomErrors to TRUE and the 404 page has a template set in the umbraco backend.

    Still the same default 404 page renders.

    Anything else I'm missing.

    Very much appreciate you helping me again!

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Sep 11, 2017 @ 06:26
    Damiaan
    0

    Hi Tobias

    Are you sure your 404 node has a template to be rendered?

    /Damiaan

  • Tobias Pettersson 19 posts 112 karma points
    Sep 11, 2017 @ 06:34
    Tobias Pettersson
    0

    I copied your 404 handler code so they look the same and I get this errormessage when I try to trigger a 404:

    enter image description here

    and when I try to preview the 404 page, through the umbraco office, i get this (wich indicates that a template is not set, but it is........).

    enter image description here

    But before, when I didn't have a template set, I got the default "Page not found" error so I know that the template really is set....

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Sep 11, 2017 @ 06:41
    Damiaan
    0

    What namespace are you using for your handler? The stacktrace (the yellow stuff) does only mention Umbraco Source Code and it is working with legacy404's... Which it should not.

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Sep 11, 2017 @ 06:42
    Damiaan
    0

    Any change you can share the project?

  • Tobias Pettersson 19 posts 112 karma points
    Sep 11, 2017 @ 07:02
    Tobias Pettersson
    0

    Hi again Damiaan,

    when I use this codesnippet:

    ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, Error404ContentFinder>();
    

    instead of the

    ContentLastChanceFinderResolver.Current.SetFinder(new PageNotFoundContentFinder());
    

    It works....everything works as it should, i get my custom 404 and it redirects as its supposed to.

    When I use the LastChanceFinderResolver my handler is never used.

    It wont hit the breakpoint in the "TryFindContent" method.

  • Tobias Pettersson 19 posts 112 karma points
    Sep 11, 2017 @ 07:10
    Tobias Pettersson
    0

    Hi again Damiaan,

    when I use this codesnippet:

    ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, Error404ContentFinder>();
    

    instead of the

    ContentLastChanceFinderResolver.Current.SetFinder(new Error404ContentFinder());
    

    *EDITED - i pasted the code so this is what it really looks like*

    It works....everything works as it should, i get my custom 404 and it redirects as its supposed to.

    When I use the LastChanceFinderResolver my handler is never used.

    It wont hit the breakpoint in the "TryFindContent" method.

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Sep 11, 2017 @ 07:12
    Damiaan
    100

    Glad you got it working.

    Still, it's not the "right" way. The issue is that there is another ContentFinder messing around. Judging from the stack trace, it is an guess is that you have "activated" the legacy not found handlers.

    Well, if it works maybe it's good enough for now?

    Kind regards
    Damiaan

Please Sign in or register to post replies

Write your reply to:

Draft