Last active
November 11, 2024 21:28
-
-
Save AaronSadlerUK/694f7016b56459bb646accc7a41f0422 to your computer and use it in GitHub Desktop.
UrlProvider to remove /home/ from published Urls in Umbraco V13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Options; | |
using Umbraco.Cms.Core.Configuration.Models; | |
using Umbraco.Cms.Core.Models.PublishedContent; | |
using Umbraco.Cms.Core.Routing; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Core.Web; | |
public class HomePageUrlProvider : DefaultUrlProvider | |
{ | |
public HomePageUrlProvider(IOptionsMonitor<RequestHandlerSettings> requestSettings, ILogger<DefaultUrlProvider> logger, ISiteDomainMapper siteDomainMapper, IUmbracoContextAccessor umbracoContextAccessor, UriUtility uriUtility, ILocalizationService localizationService) | |
: base(requestSettings, logger, siteDomainMapper, umbracoContextAccessor, uriUtility, localizationService) | |
{ | |
} | |
public override UrlInfo? GetUrl(IPublishedContent? content, UrlMode mode, string? culture, Uri current) | |
{ | |
if (content is Home) | |
{ | |
UrlInfo? defaultUrlInfo = base.GetUrl(content, mode, culture, current); | |
if (defaultUrlInfo is null) | |
{ | |
return null; | |
} | |
return !defaultUrlInfo.IsUrl ? defaultUrlInfo : new UrlInfo("/", true, defaultUrlInfo.Culture); | |
} | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Cms.Core.Composing; | |
using Umbraco.Cms.Core.DependencyInjection; | |
using Umbraco.Cms.Core.Notifications; | |
public class RegisterNotificationHandlersComposer : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
builder.AddNotificationHandler<RoutingRequestNotification, RoutingRequestNotificationHandler>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Cms.Core.Composing; | |
using Umbraco.Cms.Core.DependencyInjection; | |
public class RegisterServicesComposer : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
builder.UrlProviders().Insert<HomePageUrlProvider>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UmbHost.Agency.Shared.PublishedModels; | |
using Umbraco.Cms.Core.Events; | |
using Umbraco.Cms.Core.Notifications; | |
public class RoutingRequestNotificationHandler : INotificationHandler<RoutingRequestNotification> | |
{ | |
public void Handle(RoutingRequestNotification notification) | |
{ | |
if (notification.RequestBuilder.AbsolutePathDecoded is "/home/" or "/home") | |
{ | |
notification.RequestBuilder.SetRedirectPermanent("/"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment