Copied to clipboard

Flag this post as spam?

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


  • Peter S 169 posts 587 karma points
    Dec 28, 2016 @ 08:11
    Peter S
    0

    I just upgraded a site from 7.1.4 to 7.5.6. The site and backend works fine but there is one problem with one of my controllers. I use Umbraco.Content(1050) to get to the root node and it crashes due to a null exception. Even checking if (Umbraco != null) causes a null exception.

    What could be the problem?

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Dec 28, 2016 @ 08:14
    Dennis Adolfi
    0

    Hi Peter.

    Can you paste the code from your controller?

  • Peter S 169 posts 587 karma points
    Dec 28, 2016 @ 08:37
    Peter S
    0

    Hi,

    Here's the function crashing. It inherits from a base controller that inherits from a RenderMvcController. This worked fine in 7.1.4.

    public IPublishedContent NewsRoot() { IPublishedContent newsRoot = null;

            IPublishedContent root = Umbraco.Content(1050);
            IPublishedProperty nrProp = root.GetProperty("newsRoot");
    
            if (nrProp.HasValue)
            {
                int newsRootID = 0;
                int.TryParse(nrProp.Value.ToString(), out newsRootID);
    
                if (newsRootID > 0)
                {
                    newsRoot = Umbraco.Content(newsRootID);
                }
            }
    
            return newsRoot;
        }
    
  • Peter S 169 posts 587 karma points
    Dec 28, 2016 @ 08:40
    Peter S
    0

    I found this error on another page as well and it's where I've created new instances of the controllers to be able to use it's functions. Could this be what's causing it?

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Dec 28, 2016 @ 08:50
    Dennis Adolfi
    100

    Hi again Peter.

    Thats probobly the issue the. You should´nt create instances of your controllers in your view. Instead you should create a helper, like "NewsHelper" and create instances of that. However in a helper you are not inheriting from a controller, so you wont be able to use the "Umbraco" prexis, but instead you create your own UmbracoHelper.

    So, your helper would look like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    
    public class NewsHelper
    {
        public UmbracoHelper UmbracoHelper { get; set; }
    
        public NewsHelper()
        {
            this.UmbracoHelper = new UmbracoHelper(UmbracoContext.Current);
        }
    
        public IPublishedContent NewsRoot()
        {
            IPublishedContent newsRoot = null;
    
            IPublishedContent root = UmbracoHelper.Content(1050);
            IPublishedProperty nrProp = root.GetProperty("newsRoot");
    
            if (nrProp.HasValue)
            {
                int newsRootID = 0;
                int.TryParse(nrProp.Value.ToString(), out newsRootID);
    
                if (newsRootID > 0)
                {
                    newsRoot = UmbracoHelper.Content(newsRootID);
                }
            }
    
            return newsRoot;
        }
    }
    

    And when using is, either in a view or a controller:

                var newsHelper = new NewsHelper();
                return newsHelper.NewsRoot();
    

    Hope this can be useful!

  • Peter S 169 posts 587 karma points
    Dec 28, 2016 @ 10:13
    Peter S
    1

    Makes perfect sense. For now I'm just passing along the helper in the function just to get it up and running. But how come this worked in the earlier versions? What's new?

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Dec 28, 2016 @ 10:17
    Dennis Adolfi
    0

    Beats me, feels like it should´nt have worked in the earier version either.

    So just to be clear: did this work for you or are you still having problems? Let me know if there is anything I can help you with!

  • Peter S 169 posts 587 karma points
    Dec 28, 2016 @ 10:26
    Peter S
    1

    Yup, all good! Thanks!

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Dec 28, 2016 @ 10:27
    Dennis Adolfi
    0

    Thats great Peter! Glad to help!

    Have a good day!!

Please Sign in or register to post replies

Write your reply to:

Draft