public string ResolveUrl(string tcmUri, string pageId = null) { _logger.Debug("ResolveUrl - Start resolving url for componentId:{0} and pageId:{0}", tcmUri, string.IsNullOrEmpty(pageId) ? TcmUri.NullUri.ToString() : pageId); var resolvedUrl = string.IsNullOrEmpty(pageId) ? _linkFactory.ResolveLink(tcmUri) : _linkFactory.ResolveLink(pageId, tcmUri, TcmUri.NullUri.ToString()); //it could be a binary link. let's resolve it as a binaryLink .. if (string.IsNullOrEmpty(resolvedUrl)) { resolvedUrl = _binaryFactory.GetUrlForUri(tcmUri); } _logger.Debug("ResolveUrl - Resolved Url for componentId: {0} = {1}", tcmUri, resolvedUrl); return(resolvedUrl); }
public static MvcHtmlString ResolveRichText(this string value) { XmlDocument doc = new XmlDocument(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace("xhtml", XhtmlNamespaceUri); nsmgr.AddNamespace("xlink", XlinkNamespaceUri); var encodeValue = HttpUtility.HtmlEncode(value); doc.LoadXml(string.Format("<xhtmlroot>{0}</xhtmlroot>", encodeValue)); // resolve links which haven't been resolved foreach (XmlNode link in doc.SelectNodes("//xhtml:a[@xlink:href[starts-with(string(.),'tcm:')]][@xhtml:href='' or not(@xhtml:href)]", nsmgr)) { string tcmuri = link.Attributes["xlink:href"].Value; string linkUrl = _linkFactory.ResolveLink(tcmuri); if (!string.IsNullOrEmpty(linkUrl)) { // linkUrl = HttpHelper.AdjustUrlToContext(linkUrl); // add href XmlAttribute href = doc.CreateAttribute("xhtml:href"); href.Value = linkUrl; link.Attributes.Append(href); // remove all xlink attributes foreach (XmlAttribute xlinkAttr in link.SelectNodes("//@xlink:*", nsmgr)) { link.Attributes.Remove(xlinkAttr); } } else { //Try and get a binary url for the tcmUri if one exists IBinaryFactory bf = DependencyResolver.Current.GetService <IBinaryFactory>(); linkUrl = bf.GetUrlForUri(tcmuri); if (!string.IsNullOrEmpty(linkUrl)) { // linkUrl = HttpHelper.AdjustUrlToContext(linkUrl); // add href XmlAttribute href = doc.CreateAttribute("xhtml:href"); href.Value = linkUrl; link.Attributes.Append(href); // remove all xlink attributes foreach (XmlAttribute xlinkAttr in link.SelectNodes("//@xlink:*", nsmgr)) { link.Attributes.Remove(xlinkAttr); } } else { // copy child nodes of link so we keep them foreach (XmlNode child in link.ChildNodes) { link.ParentNode.InsertBefore(child.CloneNode(true), link); } // remove link node link.ParentNode.RemoveChild(link); } } } // remove any additional xlink attribute foreach (XmlNode node in doc.SelectNodes("//*[@xlink:*]", nsmgr)) { foreach (XmlAttribute attr in node.SelectNodes("//@xlink:*", nsmgr)) { node.Attributes.Remove(attr); } } // add application context path to images foreach (XmlElement img in doc.SelectNodes("//*[@src]", nsmgr)) { //if (img.GetAttributeNode("src") != null) // img.Attributes["src"].Value = HttpHelper.AdjustUrlToContext(img.Attributes["src"].Value); } // fix empty anchors by placing the id value as a text node and adding a style attribute with position:absolute and visibility:hidden so the value won't show up foreach (XmlElement anchor in doc.SelectNodes("//xhtml:a[not(node())]", nsmgr)) { XmlAttribute style = doc.CreateAttribute("style"); style.Value = "position:absolute;visibility:hidden;"; anchor.Attributes.Append(style); anchor.InnerText = anchor.Attributes["id"] != null ? anchor.Attributes["id"].Value : "empty"; } return(new MvcHtmlString(HttpUtility.HtmlDecode(RemoveNamespaceReferences(doc.DocumentElement.InnerXml)))); }