What about using UrlResolver
var content = UrlResolver.Current.Route(new UrlBuilder(slide.Href));
return content != null ? content.ContentLink : ContentReference.EmptyReference;
I am getting error Cannot implicitly convert type Episerver.core.contentreference to system.collection.genericelist<episerver.core.pagedata>
on this line
return content != null ? content.ContentLink : ContentReference.EmptyReference;
My whole code is like this
public static List<PageData> ToPages(this LinkItemCollection linkItemCollection) { var pages = new List<PageData>(); if (linkItemCollection == null) return pages; foreach (LinkItem linkItem in linkItemCollection) { string linkUrl; if (!PermanentLinkMapStore.TryToMapped(linkItem.Href, out linkUrl)) continue; if (string.IsNullOrEmpty(linkUrl)) continue; var pageReference = PageReference.ParseUrl(linkUrl); if (PageReference.IsNullOrEmpty(pageReference)) continue; pages.Add(DataFactory.Instance.GetPage((pageReference))); } return pages; }
Try this public static List<T> ToPages<T>(this LinkItemCollection linkItemCollection) where T : PageData { var pages = new List<T>(); if (linkItemCollection == null || linkItemCollection.Count.Equals(0)) return new List<T>(); var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>(); foreach (var linkItem in linkItemCollection) { if (String.IsNullOrEmpty(linkItem.Href)) continue; var urlBuilder = new UrlBuilder(linkItem.Href); var page = urlResolver.Route(urlBuilder) as T; if (page != null && (!page.IsDeleted && page.CheckPublishedStatus(PagePublishedStatus.Published))) { pages.Add(page); } }
I get error
Error CS0161 'LinkCollectionExtentions.ToPages<T>(LinkItemCollection)': not all code paths return a value
Looks like we are not returning anything
My other class referencing this is throwing error now.
protected override void OnLoad(EventArgs e) { base.OnLoad(e); var lic = Persons.Value as LinkItemCollection; repeatPerson.DataSource = lic.ToPages(); // this code throws error repeatPerson.DataBind(); }
Any idea?
I think you need to do like this
repeatPerson.DataSource = lic.ToPages<PageData>(); // this code throws error repeatPerson.DataBind();
Just last one thing. What is replacement for below
var url = new UrlBuilder(linkItem.Href); if (PermanentLinkMapStore.ToMapped(url)) //get warning here
This would be the equivalent
var page = urlResolver.Route(urlBuilder) as T;
Let urlresolver make the mapping between internal objekt and inernal in a linklist.
Here is my code where i tried to use your suggestion. how we make use of page variable to replace permanentlinkmapstore?
protected string Mymethod(object dataItem) { var linkItem = dataItem as LinkItem; if (linkItem == null) return ""; try { var url = new UrlBuilder(linkItem.Href); var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>(); var page = urlResolver.Route(url) as PageData; if (PermanentLinkMapStore.ToMapped(url)) { var success = EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, null, Encoding.UTF8); return url.ToString(); } return linkItem.Href; } catch (Exception) { //some logic } }
Try this
protected string Mymethod(object dataItem) { var linkItem = dataItem as LinkItem; if (linkItem == null) return ""; var content = UrlResolver.Current.Route(new UrlBuilder(linkItem.Href)); return content != null ? UrlResolver.Current.GetUrl(content.ContentLink) : linkItem.Href; }
Thanks a lot. How to mark as answer? I dont see any option. It seem only i mark my replies are answers.
The above code gives warning that PermanentLinkUtility.GetContentReference(UrlBuilder)' is obsolete: 'This method only supports classic/mapped url's which are no longer used since 8.0 - use PermanentLinkUtility.GetGuid(url) to get the unique ID from an URL'
When i try to change if condition like
Then it gives error Cannot convert EpiServer.UrlBuilder to string
What is alternative to this?
I am on Episerver 9.2