The Episerver Template Resolver Explained - How Does Episerver Call Your Controller?

As I've previously mentioned, in How to Hook Into The Episerver CMS Events Pipeline  Episerver MVC based websites are very closely related to the normal MVC pipeline, with a few surprises.  The main difference between Episerver and MVC are how incoming requests are managed.  In an Episerver website, an incoming request URL does not map to a specific controller/action, but rather the incoming request should point to a specific page type 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, after that, it's all plain sailing and the page request is sent on its merry little way.  The template resolver...  if the name doesn't give it away too much, is the thing within the pipeline that its job is to find the correct template to use for a request.  For example, when we create a controller:

public class StartPageController : PageController<StartPage>
    {
        public ActionResult Index(StartPage currentPage)
        {
            return View();
        }
    }

We use the Episerver PageController, and pass in the page type we want to match on, as T.  When we go down the inheritance stack, you'll see this class being inherited:

    [AuthorizeContent]
    [RequireClientResources]
    [TemplateDescriptor(TemplateTypeCategory = TemplateTypeCategories.MvcController)]
    [VisitorGroupImpersonation]
    public abstract class ContentController : ActionControllerBase, IRenderTemplate, IRenderTemplate where T : IContent
    {
        protected ContentController();
    }

This is how Episerver magically hooks up a lot of the controllers etc.. as long as you inherit from the correct type, all the attributes and appropriate TemplateDescriptors are added for you.  I've mainly used template resolvers when I've been working with partial views, or when I've enabled the display options feature to allow content editors to dynamically resize block widths. 

Template Resolver Events

Episerver is very developer friendly and one of the benefits of this is that, pretty much, everything that you may want to customize can be done fairly easily.  If you create an initialization module, you can hook into the template resolver events:

    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class WebsiteInitialisationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            SetDisplayOptions();
            context.Locate.TemplateResolver().TemplateResolved += MyEvent_TemplateResolved;
        }

        void MyEvent_TemplateResolved(object sender, TemplateResolverEventArgs eventArgs)
        {
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        public void Preload(string[] parameters)
        {
        }
}

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, add in extra data into the route value collection etc.. then this is a perfect time to add it.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top