Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Apr 08, 2014 @ 16:09
    Jeroen Breuer
    0

    ContentFinder and related template

    Hello,

    I'm writing my own ContentFinder and I'm already able to set the node, but when I go the correct url it only shows a blank page and not the template set on the node. I expected that it would show the correct template because the node was set on contentRequest.PublishedContent.

    Here is my code:

    public class ContentFinder : IApplicationEventHandler
    {
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, MyContentFinder>();
        }
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
    
        }
    }
    
    public class MyContentFinder : IContentFinder
    {
        #region Properties
    
        private UmbracoHelper _umbraco;
        public UmbracoHelper Umbraco
        {
            get
            {
                return _umbraco ?? (_umbraco = new UmbracoHelper(UmbracoContext.Current));
            }
        }
    
        #endregion
    
        public bool TryFindContent(PublishedContentRequest contentRequest)
        {
            try
            {
                if (contentRequest != null)
                {
                    var path = contentRequest.Uri.GetAbsolutePathDecoded();
                    var parts = path.Split(new[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length > 1)
                    {
                        var brokerParent = Umbraco.TypedContent(1136);
                        var nodeName = parts[1];
                        var node = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute("/" + brokerParent.Name + "/" + nodeName + "/");
    
                        if (node != null && node.DocumentTypeAlias == "GeneralDataBrokerDetails")
                        {
                            contentRequest.PublishedContent = node;
                            contentRequest.TrySetTemplate("TestBroker");
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                Umbraco.LogException(ex);
            }
    
            return contentRequest.PublishedContent != null;
        }
    }

    It doesn't matter if I remove the contentRequest.TrySetTemplate("TestBroker"); code. Is there something else I need to do?

    Jeroen

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Apr 08, 2014 @ 16:10
    Jeroen Breuer
    101

    I found the problem myself. Turns out that in my default controller I did some if statements and it would return null if it didn't match those statements. With the ContentFinder it didn't match those statements. After changing that to return CurrentTemplate(model); instead of null it worked :).

  • 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