In this tutorial, you will learn how Episerver hooks into the normal MVC rendering pipeline in order to map a page request to a page within Episerver. One of the big differences between an Episervered power ASP.NET website and a normal MVC ASP.NET website is the request pipeline, how incoming requests are processed. In an Episerver website, a page URL does not map to a specific controller and an action method. Instead, the Url maps to a virtual page stored within the Episerver database.

When a request comes in, Episerver needs to get the request, find the appropriate page from the database and then based on that page type, find the correct controller to use and pass the data into it. After that occurs, it's all plain sailing and the page request is sent on its merry little way like any other ASP.NET website.

The template resolvers job is to find the correct template to use for a request. For example, when we create a controller:

We a controlled inherits from the Episerver PageController base class a type can be passed in as well, as T. This passing in the type is now some of this routing magic works! When we go down the inheritance stack for PageController, you will see this class being inherited:

This is the code that allows Episerver to magically hooks up a lot of the controllers. As long as you inherit from PageContoller and pass in the correct type, all the appropriate controller attributes and TemplateDescriptors are added for you. In most situations, you will never need to know about the template resolver or use it. Template resolvers can be useful when working with partial views, or display options. To get most normal routing up and running you simply need to know about PageController

Template Resolver Events

Episerver is very developer-friendly and pretty much every API allows you to customize it in some way. The template resolver is no different as it allows you to hook into several events. Hooking into these events is fairly easy and can be done with an initialization module. After creating an initialization module, you can hook into the template resolver events like this:

In the example above, when Episerver finds an appropriate controller to use for an incoming request, it will trigger the MyEvent_TemplateResolved() method. If you want to change the controller that gets calls during a page request, this is a perfect time to add it!

Now you understand what the template resolver is, you are one step closer to being an Episerver ninja 🐱‍👤 Happy Coding 🤘