Try our conversational search powered by Generative AI!

UrlResolver.Current.Route returns null when in backoffice



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));

	throw new ArgumentNullException(nameof(target));

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

return imageData;

Aug 20, 2019 8:15

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?

Aug 20, 2019 17:20

Hi Jake,

Yes, UrlBuilder and a ContentReference.

// Carl

Aug 21, 2019 6:54

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.

Aug 21, 2019 15:18

Yes. I use ImageResizer:


And I have to use my code as an UrlBuilder extension.

Edited, Aug 21, 2019 15:21

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?

Aug 21, 2019 15:53

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

Aug 21, 2019 16:30

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

we need to change originals :)

Aug 24, 2019 0: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.