I "think" you can do a null check on what is returned from TypedMedia. If the id doesn't exist then I believe it returns null.
Personally, if I was taking that approach on the Home node (your site's root node). I would have a media picker to pick the media item to use. Then get the value from there. It is bad practice to hard code content/media id's as it is possible for them to be deleted.
Check if media exist in razor
I have the following logic I am using to load a favicon in my master template...
Is there a strong type way in wrapping this into a condition that checks if 1284 exist, then execute?
Thanks
The method
TypedMedia
will return an instance ofIPublishedContent
- you can check whether this is null like this:Hi Blackhawk,
I "think" you can do a null check on what is returned from TypedMedia. If the id doesn't exist then I believe it returns null.
Personally, if I was taking that approach on the Home node (your site's root node). I would have a media picker to pick the media item to use. Then get the value from there. It is bad practice to hard code content/media id's as it is possible for them to be deleted.
You'd possibly also want some logic to check that the media item actually still exists on the disk. This is a nice little blog post on how to do that: https://codeshare.co.uk/blog/how-to-get-the-file-path-of-a-media-item-in-umbraco/
Thanks,
Nik
Thanks for the tip on leveraging media picker rather than hard coding Id's. Much appreciated on that insight! I'm gonna give this approach a shot.
is working on a reply...