Skip to content

Instantly share code, notes, and snippets.

@stefankip

stefankip/AliasUrlProvider.cs Secret

Created Sep 1, 2020
Embed
What would you like to do?
public class AliasUrlProvider : DefaultUrlProvider
{
public AliasUrlProvider(IRequestHandlerSection requestSettings, ILogger logger, IGlobalSettings globalSettings, ISiteDomainHelper siteDomainHelper)
: base(requestSettings, logger, globalSettings, siteDomainHelper)
{
}
public override UrlInfo GetUrl(UmbracoContext umbracoContext, IPublishedContent content, UrlMode mode, string culture, Uri current)
{
var defaultUrlInfo = base.GetUrl(umbracoContext, content, mode, culture, current);
if (content == null || defaultUrlInfo == null)
{
return defaultUrlInfo;
}
var uri = new Uri(defaultUrlInfo.Text, UriKind.RelativeOrAbsolute);
string url = null;
if (!string.IsNullOrEmpty(content.GetPropertyValue(Constants.Conventions.Content.UrlAlias)))
{
url = $"{(uri.IsAbsoluteUri ? uri.GetLeftPart(UriPartial.Authority) : string.Empty)}/{content.GetPropertyValue(Constants.Conventions.Content.UrlAlias)}{(Current.Configs.Settings().RequestHandler.AddTrailingSlash ? "/" : string.Empty)}";
}
return !string.IsNullOrEmpty(url) ? new UrlInfo(url, true, defaultUrlInfo.Culture) : defaultUrlInfo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment