Reputation: 961
My variable langURL
currently returns a non-friendly url like so: http://localhost:57299/link/457fee1669e348febf67ecb57b281945.aspx?epslanguage=de-AT
Is it possible to get a friendly url?
public static IHtmlString HrefLangLinks(this PageData currentPage)
{
var availablePageLanguages = currentPage.ExistingLanguages.Select(culture => culture.Name).ToArray();
var Output = "";
var langURL = "";
foreach (string listitem in availablePageLanguages)
{
langURL = EPiServer.Web.UriUtil.AddLanguageSelection(currentPage.LinkURL, listitem);
Output += "<link href=\"" + langURL + "\" hreflang=\"" + listitem + "\" rel=\"alternate\" >";
}
// Dictionary<String, String>
return new HtmlString(Output.ToString());
}
For each page I would like to get the friendly urls with the language flag as well, for example:
Upvotes: 1
Views: 1660
Reputation: 54
Luckily we don't need to deal with the LinkURL
property anymore. Instead, I would use the UrlResolver
for this.
Your code could be quickly rewritten to something like this:
public static IHtmlString HrefLangLinks(this PageData currentPage)
{
// StringBuilder usually performs better than concatenating a variable number of strings.
var sb = new StringBuilder;
foreach (string language in currentPage.ExistingLanguages.Select(culture => culture.Name))
{
// Get the URL to the page in the individual languages, respecting the
// website language settings (sometimes a language is bound to another hostname)
string url = UrlResolver.Current.GetUrl(currentPage.ContentLink, language);
sb.AppendLine($"<link href=\"{url}\" hreflang=\"{language}\" rel=\"alternate\"/>");
}
return new MvcHtmlString(sb.ToString());
}
But I usually implement something like this as a Razor helper.
Upvotes: 2
Reputation: 1488
Yes this is possible. Try this:
public string GetExternalUrl(string linkUrl)
{
var result = string.Empty;
try
{
var url = new UrlBuilder(linkUrl);
Global.UrlRewriteProvider.ConvertToExternal(url, linkUrl, Encoding.UTF8);
result = url.ToString();
}
catch (Exception)
{
}
return result;
}
Upvotes: 1