I've looked at a couple of similar errors regarding IPublishedContent (yes I know this is a different object than what I'm using) in Umbraco, and they've all said it's been fixed since 4.11.7 - however, I'm using v6 so obviously that's not the case.
My code worked until I threw in a coalesce:
Worked:
DynamicPublishedContent countryFolder = Umbraco.Media(CurrentPage.GetProperty("contestMediaFolder").Value.ToString());
No longer works:
DynamicPublishedContent countryFolder = !string.IsNullOrEmpty(contestFolder)
? Umbraco.Media(CurrentPage.GetProperty("contestMediaFolder").Value.ToString())
: Umbraco.Media(contestFolder);
Cannot convert type 'Umbraco.Core.Dynamics.DynamicNull' to 'Umbraco.Web.Models.DynamicPublishedContent'
I have (2) controllers that are calling the same code, one is a controller that is hit on page load, the other is an API controller. This is the reason I'm passing in the variable, contestFolder, which is a string, since this method returns a list. In my method call, I'm also passing in string.Empty, which would run the first line.
I've tried:
- Replacing the strongly typed object,
DynamicPublishedContentwithvar. - Passing in an
intrather than astringto the method (contestFolderwould be an int). - Casting both returns on the
Umbraco.Media()toDynamicPublishedContent - Instead of using
DynamicPublishedContent, useIPublishedContentobject.
I switched out how I retrieved the folder. I singled out the reason, and it's b/c if regardless if that 2nd coalesce hits, Umbraco attempts to retrieve the folder, and if it's null, it returns that error,
Cannot convert type 'Umbraco.Core.Dynamics.DynamicNull' to 'Umbraco.Web.Models.DynamicPublishedContent'
Therefore, you HAVE to pass an actual media id (that's valid) to the
Umbraco.Media()method, or you'll have this error returned to you.