Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

how to get friendly urls for image properties

Vote:
 

Hi guys,

Consider you have the following image property

[UIHint("image")]
public virtual Url FeatureImage { get; set; }

Now Writing the follwing in webforms returns the non friendly url 

<%=this.currentpage.featureimage>

How can I get the friendly url of this image? (Note, i don't want to use EPiServer:Property control as using that will return friendly url but i can't to use that)

Thanks

#120628
Apr 21, 2015 13:45
Vote:
 

Have you tried:

string linkUrl = string.Empty;
		PermanentLinkMapStore.TryToMapped(CurrentBlock.FeatureImage.Uri.ToString(), out linkUrl);
#120632
Apr 21, 2015 14:39
Vote:
 

If you had used MVC you could have used @Url.ContentUrl(Model.CurrentPage.FeatureImage) but I do not know it that is possible in webform.

What you can do is to wrap that in a function that use this code:

var urlHelper = ServiceLocator.Current.GetInstance<UrlHelper>();
var friendlyUrl = urlHelper.ContentUrl([PASSED LINK TO FUNCTION]);

 

It is more explained here:

http://world.episerver.com/Forum/Developer-forum/-EPiServer-75-CMS/Thread-Container/2014/4/Get-friendly-Url-from-Url-property/

#120633
Edited, Apr 21, 2015 15:23
Vote:
 
PermanentLinkMapStore.TryToMapped(CurrentBlock.FeatureImage.Uri.ToString(), out linkUrl); returns something like /link/e5d12600-ec7d-4592-a124-f08a525caa94.aspx?id=1111&epslanguage=en

There is no method ContentUrl on UrlHelper class.Also, None of other methods mentioned in other thread worked :(. 

All i am doing is on model

[UIHint(UIHint.Image)]

public virtual Url UrlToImage{get;set;}

and in the aspx page

<%=this.CurrentPage.UrlToImage %>


but it shows something like /link/e5d12600-ec7d-4592-a124-f08a525caa94.aspx?id=1111&epslanguage=en

Property control renders it correctly by showing something /global/a.jpg.

but i dont want (can't) use property control.

Any other suggestions guys?

#120828
Apr 24, 2015 16:34
Vote:
 

The following code sample should give you friendly url. (there is no error checking in this code)

 var urlHelper = ServiceLocator.Current.GetInstance<UrlResolver>();
var friendlyUrlBuilder = new UrlBuilder(this.CurrentPage.NewLogoUrl.ToString());
ContentReference contentReference = PermanentLinkUtility.GetContentReference(friendlyUrlBuilder);
 string url = urlHelper.GetUrl(contentReference);

return url;

I am unsure if this is the best way of doing it but it seems to be working fine.

#120833
Apr 24, 2015 17:08
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.