When you build a website that is powered by Episerver CMS, a very common requirement will be to call a non-Episerver controller. Routing within a CMS framework is different than traditional MVC routing. If you're new to Episerver and you're struggling with your routing, then this article is for you. First, don't sweat it.. getting routing to work can be frustrating! I suggest you read Episerver Routing For Dummies to get a basic understanding of what's going on behind the scenes.

Episerver applies some custom routes into the routing table to hook up the correct action and controller on each page request in the background. A URL on your website links to a page. In Episerver a website is built up from page types and content added in the content tree. A URL will not simply map to a controller and an action. Instead, it maps to some data stored within a database. To get a normal MVC/partial controller to route successfully within Episerver you need to add a custom rule with the routing table. This rule could look like this:

This rule defines a name (which can be anything), a URL the rule relates to and an action that should be called on a successful match. Doing this will allow you to use a vanilla MVC controller in Episerver.

On a recent project, someone on the team pointed out that the base controller for both the PageController and the BlockController, both inherit from something called ActionControllerBase. Doing some Google research it doesn't look like many people have written about ActionControllerBase, so here goes

What Is ActionControllerBase?

ActionControllerBase isn't really anything amazingly special, however, if you want to use a vanilla MVC controller in your project you may be able to use it to make your life a little easier. As I mentioned in the introductory blurb, ActionControllerBase is used by the Episerver PageController and BlockController. The benefit of ActionControllerBase is that it will work with the routing table without the need for defining a custom rule. If you need to use a standard MVC controller, instead of having to manually hook up the routing yourself, like in the example above, you can inherit your controller from ActionControllerBase, name your controller and index as normal and in your code and your routing should magically hook up and work.

Creating A Partial Controller

Creating a partial controller is also simple. Let us see the code:

This code should allow the controllers routing to work, without you needing to touch your route.config:

If you need to use normal MCV controllers in Episerver and you're having issues with the routing and getting your controller to work, then inherit from ActionControllerBase. Using ActionControllerBase should mean that Episerver sorts out the routing rules for you and you can use normal MVC calls to access your controllers. Happy Coding 🤘