Remove multiple dashes in Episerver automatic generated urls

When Episerver automatically generates url for a page in the CMS you will get multiple dashes (-) if there is a dash with a space before or after in the page name.

Episerver url with multiple dashes

Episerver url with multiple dashes

Episerver url with multiple dashes

If you don't like that there is a way to replace the multiple dashes with a single one by creating an Initialization module and hook up on the event which is fired after the url is created. The pre Episerver v10 way to do this was using the UrlSegment class and hooking up on the CreatedUrlSegment event and modify the url segment. Joel Abrahamsson wrote a blog post about it.

Episerver announced before the v10 release that some of their APIs would change to internal ones. Introducing changes to reduce our Public API and the UrlSegment class is now moved to an Internal namespace and the obsolete message tells us to use the IUrlSegmentCreator instead.

After upgrading an Episerver 9 project to Episerver 10 I got some build warnings about this and below is what the code looks like after the rewrite. Note that the InitializationEngine now contains a ServiceLocationHelper property called Locate which is used to get the IUrlSegmentCreator instance. Read more about Dependency Injection within Initializable Modules

    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class UrlSegmentHandlingInitialization : IInitializableModule
    {
        private bool _eventAttached;
        public void Initialize(InitializationEngine context)
        {
            if (_eventAttached)
            {
                return;
            }

            var urlSegmentCreator = context.Locate.Advanced.GetInstance<IUrlSegmentCreator>();
            urlSegmentCreator.Created += OnCreatedUrlSegment;
            _eventAttached = true;
        }

        private void OnCreatedUrlSegment(object sender, UrlSegmentEventArgs urlSegmentEventArgs)
        {
            var segment = urlSegmentEventArgs.RoutingSegment.RouteSegment;

            while (segment.Contains("--"))
            {
                segment = segment.Replace("--", "-");
            }

            urlSegmentEventArgs.RoutingSegment.RouteSegment = segment;
        }

        public void Uninitialize(InitializationEngine context)
        {
            var urlSegmentCreator = context.Locate.Advanced.GetInstance<IUrlSegmentCreator>();
            urlSegmentCreator.Created -= OnCreatedUrlSegment;
        }
    }

If you are upgrading from Episerver 9 to Episerver 10, mind the Obsolete message on the old events. Both CreatingUrlSegment and CreatedUrlSegment has the same message which tells you to use the Creating event.

[Obsolete("Use IUrlSegmentCreator.Creating instead")]

If you were using the CreatedUrlSegment event you should of course use the IUrlSegmentCreator.Created and not the Creating event.

This is what the automatically generated url looks like with the InitializableModule in place.

Episerver url without multiple dashes

Much prettier isn't it :)

comments powered by Disqus