DEV Community

Martin Soderlund Ek
Martin Soderlund Ek

Posted on

Partial Routing in Optimizely 12

If you need to implement partial routing in Optimizely CMS 12, this is a good start.

There are of course some breaking changes as per the documentation:

The interface IPartialRouter changed slightly, and implementations should now be registered in DI container as IPartialRouter, rather than the previous way to register a partial router, which was through the extension method RegisterPartialRoute on RouteCollection.

OK, lets start with adding a partial router class which implements IPartialRouter:

public class MyPagePartialRouter : IPartialRouter<MyPage, MyPage>
    {
        public MyPagePartialRouter() { }

        public object RoutePartial(MyPage content, UrlResolverContext urlResolverContext)
        {
            urlResolverContext.RouteValues.Add("requestPath", urlResolverContext.RemainingSegments);
            urlResolverContext.RemainingSegments = Array.Empty<Char>();
            return content;
        }

        public PartialRouteData GetPartialVirtualPath(MyPage content, UrlGeneratorContext urlGeneratorContext)
        {
            return null;
        }
    }
Enter fullscreen mode Exit fullscreen mode

We add our params to the RouteValues collection. Note we use RemainingSegments instead of RemainingPath.

Then we need to register it in a DI container like this:

services.AddSingleton<IPartialRouter, MyPagePartialRouter>();

And that's it!

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay