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.
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?
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();
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?
Umbraco is null
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?
Hi Peter.
Can you paste the code from your controller?
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;
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?
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:
And when using is, either in a view or a controller:
Hope this can be useful!
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?
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!
Yup, all good! Thanks!
Thats great Peter! Glad to help!
Have a good day!!
is working on a reply...