Quantcast
Viewing all articles
Browse latest Browse all 9076

UrlResolver.Current.Route returns null when in backoffice

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;

Viewing all articles
Browse latest Browse all 9076

Trending Articles