Skip to content

Instantly share code, notes, and snippets.

@stefankip
Created September 1, 2020 10:59
Show Gist options
  • Save stefankip/e00d50a5c4711a29269c88049fc1976b to your computer and use it in GitHub Desktop.
Save stefankip/e00d50a5c4711a29269c88049fc1976b to your computer and use it in GitHub Desktop.
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