UrlResolver.Current.Route returns null when in backoffice

Vote:
 

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?

if (target == null || target.IsEmpty)
	throw new ArgumentNullException(nameof(target));

var content = UrlResolver.Current.Route(new UrlBuilder(target.Path));
if (content == null) 
	throw new ArgumentNullException(nameof(target));

if(ContentReference.IsNullOrEmpty(content.ContentLink))
	throw new ArgumentNullException(nameof(target));

ImageFile imageData;
try
{
	imageData = ContentRepository.Service.Get<ImageFile>(content.ContentLink);
}
catch (TypeMismatchException)
{
	throw new ArgumentNullException(nameof(target));
}

return imageData;

#206401
Aug 20, 2019 8:15
Vote:
 

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 usinga ContentReference, a Url, a LinkItemCollection or something else?

#206415
Aug 20, 2019 17:20
Vote:
 

Hi Jake,

Yes, UrlBuilder and a ContentReference.

// Carl

#206428
Aug 21, 2019 6:54
Vote:
 

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.

#206450
Aug 21, 2019 15:18
Vote:
 

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.

#206451
Edited, Aug 21, 2019 15:21
Vote:
 

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?

#206452
Aug 21, 2019 15:53
Vote:
 

I don't have access to the Resizer original extension and the Html.ResizeImage takes a ContentReference.

#206456
Aug 21, 2019 16:30
Vote:
 

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;
    }
}
#206457
Aug 21, 2019 16:46
Vote:
 

we need to change originals :)

#206559
Aug 24, 2019 0:58