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!

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay