yes that's one of the option. I would hide it behind some extension method and not access content loader via service locator anti-pattern. and btw, I would create an extension on IContentLoader interface, instead of ContentReference just to avoid service locator. and would override webpage base type (aside from aspnet MVC default supplied) to inject automatically required interfaces "directly" into a view.
But... that's the whole new story around dependency injection topic.
What about a custom ImageFile property? I tried to create one, but I wasn't able to have it resemble af ContentRefence, it always ended up as a multiline text field.
in order to have reference to media you have to have property of type ContentReference - https://world.episerver.com/documentation/developer-guides/CMS/Content/Properties/built-in-property-types/
Yes, but I was hoping I could make a custom property that took an asset (my custom ImageFile content), but I guess I can't.
what about this? I washed my hands after wrote this :)
public ImageFile Image => ProductImage == ContentReference.EmptyReference ? null : ServiceLocator.Current.GetInstance<IContentRepository>().Get<ImageFile>(ProductImage);
Hey
We've implemented our own ImageFile class below:
When I want some content to have an image property I do the following:
On a simple BlockData type, we don't have controller cause it is simple rendering, so it just goes straight to the view.
However now my ProductImage is a ContentReference in the view and of course not an ImageFile with the AltText property. As I see it I have two options:
My question is; is there no other/better way to always access my ContentReferences for images as ImageFiles?
Thank you in advance!