public static ContentReference GetContentReference(this LinkItem linkItem) { string extension; var guid = PermanentLinkUtility.GetGuid(new UrlBuilder(linkItem.GetMappedHref()), out extension); return(PermanentLinkUtility.FindContentReference(guid)); }
public static IHtmlString RawButWithMappedUrls(this HtmlHelper helper, string value) { var formattedString = value; var linkFragments = new HtmlStreamReader(value).OfType <ElementFragment>().Where(f => f.NameEquals("a")).ToList(); if (linkFragments.Any()) { var linkMapper = ServiceLocator.Current.GetInstance <IPermanentLinkMapper>(); foreach (var linkFragment in linkFragments) { if (linkFragment.HasAttributes) { var attribute = linkFragment.Attributes["href"]; if (PermanentLinkUtility.IsMappableUrl(new UrlBuilder(attribute.UnquotedValue))) { var url = new UrlFragment(attribute.UnquotedValue, linkMapper); var mappedUrl = MapUrlFromRoute(helper.ViewContext.RequestContext, helper.RouteCollection, url.GetViewFormat()); if (!string.IsNullOrEmpty(mappedUrl)) { formattedString = value.Replace(attribute.UnquotedValue, mappedUrl); } } } } } return(new HtmlString(formattedString)); }
private List <MultipleJsonImage> ConvertImagesToList(string serializedString) { var images = new List <MultipleJsonImage>(); if (!string.IsNullOrEmpty(serializedString)) { images = JsonConvert.DeserializeObject <List <MultipleJsonImage> >(serializedString); } var contentLoader = ServiceLocator.Current.GetInstance <IContentLoader>(); foreach (var item in images) { try { var guid = PermanentLinkUtility.GetGuid(item.PermanentUrl); var contentRef = PermanentLinkUtility.FindContentReference(guid); var image = contentLoader.Get <ImageFile>(contentRef); item.ContentLink = contentRef; item.Image = image; } catch (Exception e) { //TODO: log exception... } } return(images); }
public ActionResult Index() { var item = this._urlKeeperRepository.GetByOldPath(Request.RawUrl); if (item != null) { try { var redirectUrl = this._urlResolver.GetUrl(PermanentLinkUtility.FindContentReference(item.ContentGuid)); if (redirectUrl != null) { Response.RedirectPermanent(redirectUrl, true); } } catch { } } var pageNotFoundTitle = CommonHelpers.TranslateFallback("/views/pagenotfound/title", "Sorry"); var pageNotFoundText = CommonHelpers.TranslateFallback("/views/pagenotfound/text", "404\nThat page\ndoesn't seem\nto exist"); var model = new NotFoundViewModel { Title = pageNotFoundTitle, Text = pageNotFoundText }; Response.TrySkipIisCustomErrors = true; Response.StatusCode = 404; return(View(model)); }
public static IEnumerable <T> ToContentItems <T>(this LinkItemCollection links, IContentRepository repo) where T : ContentData { if (links == null) { yield break; } foreach (var link in links) { var linkUrl = new UrlBuilder(link.Href); if (!PermanentLinkMapStore.ToMapped(linkUrl)) { continue; } var contentLink = PermanentLinkUtility.GetContentReference(linkUrl); var item = repo.Get <T>(contentLink); if (item != null) { yield return(item); } } }
/// <summary> /// Returns ContentReference for provided LinkItem if it is EPiServer page otherwise returns EmptyReference. /// </summary> /// <param name="source">Source LinkItem for which to return content reference.</param> /// <returns>Returns ContentReference for provided LinkItem if it is EPiServer page otherwise returns EmptyReference.</returns> public static ContentReference ToContentReference(this LinkItem source) { var urlBuilder = new UrlBuilder(source.Href); return(PermanentLinkMapStore.ToMapped(urlBuilder) ? PermanentLinkUtility.GetContentReference(urlBuilder) : ContentReference.EmptyReference); }
private static void AddUriToIndexItem(IContent content, IndexRequestItem item) { string url = PermanentLinkUtility.GetPermanentLinkVirtualPath(content.ContentGuid, ".aspx"); var locale = content as ILocale; if (locale != null && locale.Language != null) { url = UriSupport.AddLanguageSelection(url, locale.Language.Name); } item.Uri = new Url(url).Uri; }
/// <summary> /// Get a page reference from the url /// </summary> /// <param name="url"></param> /// <returns></returns> public static int GetReferenceFromUrl(Url url) { if (url != null) { var reference = PermanentLinkUtility.GetContentReference(new UrlBuilder(url.ToString())); if (reference != null && reference.ID > 0) { return(reference.ID); } } return(-1); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string guid = context.Request.QueryString.ToString(); if (!string.IsNullOrEmpty(guid)) { Guid g = new Guid(guid); ContentReference cr = PermanentLinkUtility.FindContentReference(g); string url = UrlResolver.Current.GetUrl(cr); context.Response.Write(url); } }
/// <summary> /// Prepares all links in a LinkItemCollection for output /// by filtering out inaccessible links and ensures all links are correct. /// </summary> /// <param name="linkItemCollection">The collection of links to prepare.</param> /// <param name="targetExternalLinksToNewWindow">True will set target to _blank if target is not specified for the LinkItem.</param> /// <returns>A prepared and filtered list of LinkItems</returns> public static IEnumerable <LinkItem> ToPreparedLinkItems(this LinkItemCollection linkItemCollection, bool targetExternalLinksToNewWindow) { var contentLoader = ServiceLocator.Current.GetInstance <IContentLoader>(); if (linkItemCollection != null) { foreach (var linkItem in linkItemCollection) { var url = new UrlBuilder(linkItem.Href); if (PermanentLinkMapStore.ToMapped(url)) { var pr = PermanentLinkUtility.GetContentReference(url); if (!PageReference.IsNullOrEmpty(pr)) { // page var page = contentLoader.Get <PageData>(pr); if (IsPageAccessible(page)) { linkItem.Href = page.LinkURL; yield return(linkItem); } } else { // document if (IsFileAccessible(linkItem.Href)) { Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.Encoding.UTF8); linkItem.Href = url.Path; yield return(linkItem); } } } else if (!linkItem.Href.StartsWith("~")) { // external if (targetExternalLinksToNewWindow && string.IsNullOrEmpty(linkItem.Target)) { linkItem.Target = "_blank"; } if (linkItem.Href.StartsWith("mailto:") || linkItem.Target == "null") { linkItem.Target = string.Empty; } yield return(linkItem); } } } }
private void SaveSoftLinks(IContent content) { var contentData = content as IContentData; if (contentData == null) { return; } foreach (var propertyData in contentData.Property) { var data = propertyData as PropertyMarkdown; if (data != null) { var markdownAsHtml = MarkdownService.Transform(data.Value as string); var htmlReader = new HtmlStreamReader(markdownAsHtml); var linkFragments = htmlReader.OfType <ElementFragment>().Where(f => f.NameEquals("a")).ToList(); if (linkFragments.Any()) { var softLinks = new List <SoftLink>(); foreach (var elementFragment in linkFragments) { if (elementFragment.HasAttributes) { var attribute = elementFragment.Attributes["href"]; if (IsValidLinkAttribute(attribute) && PermanentLinkUtility.IsMappableUrl(new UrlBuilder(attribute.UnquotedValue))) { softLinks.Add(new SoftLink { OwnerContentLink = content.ContentLink.CreateReferenceWithoutVersion(), OwnerLanguage = content is ILocalizable ? ((ILocalizable)content).Language : null, SoftLinkType = ReferenceType.PageLinkReference, Url = attribute.UnquotedValue }); } } } ContentSoftLinkRepository.Save(content.ContentLink.CreateReferenceWithoutVersion(), content is ILocalizable ? ((ILocalizable)content).Language : null, softLinks); } } } }
private static string MapUrlFromRoute(RequestContext requestContext, RouteCollection routeCollection, string url) { var mappedUrl = new UrlBuilder(HttpUtility.HtmlDecode(url)); var contentReference = PermanentLinkUtility.GetContentReference(mappedUrl); if (ContentReference.IsNullOrEmpty(contentReference)) { return(mappedUrl.ToString()); } var language = mappedUrl.QueryCollection["epslanguage"] ?? (requestContext.GetLanguage() ?? ContentLanguage.PreferredCulture.Name); var setIdAsQueryParameter = false; bool result; if (!string.IsNullOrEmpty(requestContext.HttpContext.Request.QueryString["idkeep"]) && !bool.TryParse(requestContext.HttpContext.Request.QueryString["idkeep"], out result)) { setIdAsQueryParameter = result; } return(routeCollection.GetVirtualPath(contentReference, language, setIdAsQueryParameter, false).GetUrl()); }
/// <summary> /// Converts a LinkItemCollection to typed pages. Any non-pages will be filtered out. (Not compatible with PageList - Use ToPageDataList) /// </summary> /// <typeparam name="T">PageType</typeparam> /// <param name="linkItemCollection">The collection of links to convert</param> /// <returns>An enumerable of typed PageData</returns> public static IEnumerable <T> ToPages <T>(this LinkItemCollection linkItemCollection) where T : PageData { var contentLoader = ServiceLocator.Current.GetInstance <IContentLoader>(); if (linkItemCollection != null) { foreach (var linkItem in linkItemCollection) { var url = new UrlBuilder(linkItem.Href); if (PermanentLinkMapStore.ToMapped(url)) { var pr = PermanentLinkUtility.GetContentReference(url); if (!PageReference.IsNullOrEmpty(pr)) { var page = contentLoader.Get <PageData>(pr); if (page is T && IsPageAccessible(page)) { yield return((T)page); } } } } } }
public ActionResult Submit(LikeButtonBlockViewModel likeButtonBlock) { var targetPageRef = Reference.Create(PermanentLinkUtility.FindGuid(likeButtonBlock.Link).ToString()); var raterUserRef = GetRaterRef(); try { // Add the rating using the Episerver Social Rating service var addedRating = _ratingService.Add( new Rating( raterUserRef, targetPageRef, new RatingValue(LikedRating) ) ); } catch (Exception) { // The rating service may throw a number of possible exceptions // should handle each one accordingly -- see rating service documentation } return(Redirect(UrlResolver.Current.GetUrl(likeButtonBlock.Link))); }
private void ModifyIContentProperties(LinkItem serverModel, ExtendedEPiLinkModel clientModel) { var mappedHref = serverModel.GetMappedHref(); if (string.IsNullOrEmpty(mappedHref)) { return; } var hrefWithoutHash = mappedHref; var anchorOnPage = ""; var indexOfHash = mappedHref.IndexOf('#'); if (indexOfHash > 0) { hrefWithoutHash = mappedHref.Substring(0, indexOfHash - 1); anchorOnPage = mappedHref.Substring(indexOfHash + 1); } clientModel.Href = hrefWithoutHash; clientModel.AnchorOnPage = anchorOnPage; var contentGuid = PermanentLinkUtility.GetGuid(hrefWithoutHash); var contentReference = PermanentLinkUtility.FindContentReference(contentGuid); IContent content; if (!(contentReference != ContentReference.EmptyReference) || !_contentRepository.TryGet(contentReference, out content)) { return; } clientModel.TypeIdentifier = _uiDescriptors.GetTypeIdentifiers(content.GetType()).FirstOrDefault(); var friendlyUrl = _urlHelper.ContentUrl(content.ContentLink); var absoluteUriBySettings = UriSupport.AbsoluteUrlBySettings(friendlyUrl); clientModel.PublicUrl = indexOfHash > 0 ? string.Format("{0}#{1}", absoluteUriBySettings, anchorOnPage) : absoluteUriBySettings; }
public ContentReference GetContentReferenceFromGuid(Guid guid) { return(PermanentLinkUtility.FindContentReference(guid)); }
/// <summary> /// Render the Like button block frontend view. /// </summary> /// <param name="currentBlock">The current block instance.</param> /// <returns>Result of the redirect to the current page.</returns> public override ActionResult Index(LikeButtonBlock currentBlock) { var pageLink = _pageRouteHelper.PageLink; var targetPageRef = Reference.Create(PermanentLinkUtility.FindGuid(pageLink).ToString()); var anonymousUser = User.Identity.GetUserId() == null ? true : false; // Create a rating block view model to fill the frontend block view var blockModel = new LikeButtonBlockViewModel(currentBlock) { Link = pageLink }; try { // Using the Episerver Social Rating service, get the existing rating for the current // user (rater) and page (target). This is done only if there's a user identity. Anonymous // users will never match a previously submitted anonymous Like rating as they are always // uniquely generated. if (!anonymousUser) { var raterUserRef = GetRaterRef(); var ratingPage = _ratingService.Get( new Criteria <RatingFilter> { Filter = new RatingFilter { Rater = raterUserRef, Targets = new List <Reference> { targetPageRef } }, PageInfo = new PageInfo { PageSize = 1 } } ); // Add the current Like rating, if any, to the block view model. If the user is logged // into the site and had previously liked the current page then the CurrentRating value // should be 1 (LIKED_RATING). Anonymous user Likes are generated with unique random users // and thus the current anonymous user will never see a current rating value as he/she // can Like the page indefinitely. if (ratingPage.Results.Count() > 0) { blockModel.CurrentRating = ratingPage.Results.ToList().FirstOrDefault().Value.Value; } } // Using the Episerver Social Rating service, get the existing Like statistics for the page (target) var ratingStatisticsPage = _ratingStatisticsService.Get( new Criteria <RatingStatisticsFilter> { Filter = new RatingStatisticsFilter { Targets = new List <Reference> { targetPageRef } }, PageInfo = new PageInfo { PageSize = 1 } } ); // Add the page Like statistics to the block view model if (ratingStatisticsPage.Results.Count() > 0) { var statistics = ratingStatisticsPage.Results.ToList().FirstOrDefault(); if (statistics.TotalCount > 0) { blockModel.TotalCount = statistics.TotalCount; } } } catch (Exception) { // The rating service may throw a number of possible exceptions // should handle each one accordingly -- see rating service documentation } return(PartialView("~/Features/Blocks/Views/LikeButtonBlock.cshtml", blockModel)); }
public static ContentReference GetContentReference(Guid contentGuid) { return(PermanentLinkUtility.FindContentReference(contentGuid) ?? ContentReference.EmptyReference); }