Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Hi Carl,
I think I need a little more context to work out what's going on here.
What is target
? I feel like it must be a UrlBuilder
based on the Path
and IsEmpty
properties. Also, presumably the editor is selecting this image in the CMS? What property are you using—a ContentReference
, a Url
, a LinkItemCollection
or something else?
Is there a reason you don't just get the ImageFile
from the the ContentReference directly? This seems a very roundabout way to get there.
Yes. I use ImageResizer:
@Html.ResizeImage(Model.VerticalImage.Image).Width(700).Height(1031).FitMode(FitMode.Crop)
And I have to use my code as an UrlBuilder extension.
I suppose when you initally resolve the URL you could ensure you never get the edit mode URL by specifying UrlResolverArguments, something like:
var url = _urlResolver.GetUrl(page.PageLink, ContentLanguage.PreferredCulture.Name,
new UrlResolverArguments {ContextMode = ContextMode.Default});
Honestly, I feel like there still must be a better approach, maybe adding a new overload method for ResizeImage since that takes in a ContentReference already?
I don't have access to the Resizer original extension and the Html.ResizeImage takes a ContentReference.
Would you need to change the original? Maybe you could just do something like this (untested) example:
public static class HtmlHelperExtensions
{
static Injected<IContentRepository> ContentRepository { get; set; }
public static UrlBuilder ResizeImageCustom(this HtmlHelper helper, ContentReference image, int? width = null, int? height = null)
{
if (ContentReference.IsNullOrEmpty(image))
{
throw new ArgumentNullException(nameof(image));
}
ImageFile imageData;
ContentRepository.Service.TryGet(image, out imageData);
var urlBuilder = helper.ResizeImage(image, width, height);
if (imageData != null)
{
// Do whatever needs to be done.
}
return urlBuilder;
}
}
Hi,
I'm using a UrlBuilder extension to handle some images. However the UrlResolver.Current.Route returns null when I preview in the backoffice.
The target.Path in backoffice is "/EPiServer/CMS/Content/globalassets/en/about_us_1100_1620.jpg,,50926" - but I'm guessing this is already a contentreference?
The target.Path outside backoffice is "/globalassets/about_us_1100_1620.jpg" and this have no problem converting to a contentreference.
How can I fix this so I can use it both in backoffice and "outside"?
One way is to split the target.Path on ',' and use "new ContentReference(int.Parse(splittedValue[1]))" but that seems ugly. Any nicer solutions?