Opticon Stockholm is on Tuesday September 10th, hope to see you there!
Opticon Stockholm is on Tuesday September 10th, hope to see you there!
PM> Install-Package Geta.EPi.Extensions
and
@Model.CurrentPage.SomeContentLink.GetFriendlyUrl(includeHost: true, language: "{your-language-code}")
There's an even easier way to generate absolute URLs nowadays:
ServiceLocator.Current.GetInstance<UrlResolver>(contentReference, new VirtualPathArguments { ForceAbsolute = true });
But please don't use the service locator ;) If you want to use this in a view you could easily create an extension method with above code or just use it in the code were you construct your view model.
Oh this is nice. I didn't know that you added this feature to the product.
Nice. Which version of the EPiServer.Cms.AspNet package was ForceAbsolute introduced in, Johan? The release notes for that package are not that great..
We could definiately improve our release information. Not all features have public work items and some features are sometimes delivered as part of others. This was released back in May in EPiServer.CMS.Core 11.15.1 https://world.episerver.com/documentation/Release-Notes/?versionFilter=11.15.1&packageFilter=EPiServer.CMS.Core&typeFilter=All.
Absolute URL support was added since it was needed in Content Delivery API, so there might be release notes in that package regarding this.
Thanks. I used @Johan Petersson's method to create the following extension:
public static Uri AbsoluteUri(this ContentReference contentReference, CultureInfo language = null)
{
var lang = language == null ? EPiServer.Globalization.ContentLanguage.PreferredCulture : language;
var urlString = UrlResolver.Current.GetUrl(contentReference, lang.Name, new VirtualPathArguments { ForceAbsolute = true });
// Format and check to make sure the URI is absolute.
// If it is, return it, if it's not, force it to be
var uri = new Uri(urlString, UriKind.RelativeOrAbsolute);
if (uri.IsAbsoluteUri)
return uri;
// force an absolute URI based on lang
return new Uri(GetPrimaryHostname(language), uri);
}
private static Uri GetPrimaryHostname(CultureInfo language = null)
{
Uri retVal = null;
var lang = language ?? System.Threading.Thread.CurrentThread.CurrentCulture;
foreach (var host in SiteDefinition.Current.Hosts)
{
if (host.Type == HostDefinitionType.Primary && host.Language.Equals(lang))
{
retVal = host.Url;
break;
}
}
if (retVal == null)
retVal = SiteDefinition.Current.Hosts.FirstOrDefault(x => x.Type == HostDefinitionType.Primary)?.Url;
return retVal;
}
Hi,
I have an episerver website offered in two languages: en and en-ca. Both languages share the same content hierarchy. I have configured en-ca language to fall back to en.
My site configuration looks like this:
Everything appears to be fine, and when I navigate to a page on mysite.ca I see content offered in en-ca and falling back to en. I am noticing, however, that certain URLs are being generated back to mysite.com.
Most URLs are being generated as relative, and these are working as expected. For some URLs, however, I want to include the hostname within the URL. Canonical links, Open Graph links, sitemap links, etc- we want to all have hostnames associated with them. What is the proper mechanism for generating a URL with the hostname inside of it while taking language into account?
@Url.ContentUrl appears to be strictly relative.