namespace Project { using System; using System.Text; using System.Web; using EPiServer; using EPiServer.Core; using EPiServer.SpecializedProperties; using EPiServer.Web; public static class WebUrlHelper { public static string BuildLinkHtml(PageReference pageReference, string text, string title = null, string cssClass = null, bool fullyQualified = false, bool newWindow = false) { string linkUrl = "#"; if (!PageReference.IsNullOrEmpty(pageReference)) { PageData pageData = DataFactory.Instance.GetPage(pageReference); PropertyFrame propertyFrame = pageData.Property["PageTargetFrame"] as PropertyFrame; if (!newWindow && !string.IsNullOrEmpty(propertyFrame.FrameName) && string.Equals(propertyFrame.FrameName, "_blank", StringComparison.OrdinalIgnoreCase)) { newWindow = true; } linkUrl = GetPageUrl(pageData, fullyQualified).ToString(); } return BuildLinkHtml(linkUrl, text, title, cssClass, newWindow); } public static string BuildLinkHtml(string linkUrl, string text, string title = null, string cssClass = null, bool newWindow = false) { if (!string.IsNullOrEmpty(title)) title = string.Format(" title=\"{0}\"", title); if (!string.IsNullOrEmpty(cssClass)) cssClass = string.Format(" class=\"{0}\"", cssClass); return string.Format("{4}", linkUrl, cssClass, title, newWindow ? " target=\"_blank\"" : string.Empty, text); } public static Uri GetPageUrl(PageData pageData, bool fullyQualified = false) { PageShortcutType propertyLinkType = (PageShortcutType)Enum.Parse(typeof(PageShortcutType), pageData.Property["PageShortcutType"].ToString()); UrlBuilder url = new UrlBuilder(pageData.LinkURL); bool getPageUrl = true; bool changeHostAndScheme = true; if (propertyLinkType == PageShortcutType.Shortcut && pageData.LinkURL.IndexOf("id=", StringComparison.OrdinalIgnoreCase) != -1) { string id = pageData.LinkURL.Substring(pageData.LinkURL.IndexOf("id=", StringComparison.OrdinalIgnoreCase) + 3); if (id.Contains("&")) id = id.Substring(0, id.IndexOf("&", StringComparison.OrdinalIgnoreCase)); int pageId; if (int.TryParse(id, out pageId)) { pageData = DataFactory.Instance.GetPage(new PageReference(pageId)); url = new UrlBuilder(pageData.LinkURL); } } if (propertyLinkType == PageShortcutType.External) { getPageUrl = false; changeHostAndScheme = false; } if (UrlRewriteProvider.IsFurlEnabled && getPageUrl) Global.UrlRewriteProvider.ConvertToExternal(url, pageData.PageLink, Encoding.UTF8); if (changeHostAndScheme && fullyQualified && HttpContext.Current != null) { url.Host = HttpContext.Current.Request.Url.Host; url.Scheme = HttpContext.Current.Request.Url.Scheme; } return url.Uri; } } }