Try our conversational search powered by Generative AI!

Using custom ImageFile class in CMS v11.9.4



We've implemented our own ImageFile class below:

    [ContentType(GUID = "868493e9-062f-49fd-98e0-6580dbf17ce7")]
    [MediaDescriptor(ExtensionString = "jpg,jpeg,png,gif")]
    public class ImageFile : ImageData
        public virtual string AltText { get; set; }

When I want some content to have an image property I do the following:

            GroupName = GroupNames.CampaignHeroGroupNames.Product,
            Order = 20)]
        public virtual ContentReference ProductImage { get; set; }

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:

  1. In the view, get the IContentLoader service and _contentLoader.Get(Model.ProductImage).
  2. Implement a controller and viewmodel for every simple block type which sole purpose is _contentLoader.Get(Model.ProductImage).

My question is; is there no other/better way to always access my ContentReferences for images as ImageFiles?

Thank you in advance!

Nov 05, 2018 17:31

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.

Nov 07, 2018 14:13

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.

Nov 07, 2018 14:28

in order to have reference to media you have to have property of type ContentReference -

Nov 08, 2018 9:24

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.

Nov 08, 2018 11:58

what about this? I washed my hands after wrote this :)

public ImageFile Image => ProductImage == ContentReference.EmptyReference ? null : ServiceLocator.Current.GetInstance<IContentRepository>().Get<ImageFile>(ProductImage);
Nov 08, 2018 12:43

Thank you

Nov 08, 2018 13:58
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.