In this tutorial, you will get access to all the code that is required to manually render an Episerver page or block. The first thing I will say is that manually rendering Episerver items is definitely for edge case scenarios only. Unless you have a specific need, use the normal techniques to render pages. If you do not know how to do that read this
If you need this code, my advice would be to strongly consider if the design of your architecture is correct. In almost every situation, it's a lot easier to let Episerver and the Episerver API do the hard work for you. After that warning, if you are still interested in how Episerver hi-jacks the MVC pipeline and renders virtual pages this is the tutorial for you 🔥🔥🔥
How To Manually Render Episerver Code
In order to manually render a page or a block, the first thing you will need to do is manually generate the data that Episerver would normally pass into the controller and a few other little gems of data. This includes the Episerver page, a view model (I assume you use them!), the controller to use, the action to invoke and the site's Url. To make it super-clear how all this works, lets first define the basic episerver building blocks that I will use in this tutorial:
Page Type Definition: We will need a page to render. An example page type definition class is shown below:
View Model: We need a view model:
The Controller: The page will need a corresponding page controller:
We will skip the view. We now have the basics defined to call a page and render some data.
What Data Is Required To Manually Call An Episerver Page?
To render a page, you will need the following bits of data:
The Page ID
A View Model
The Page Controller Type
The Action To Invoke
NOTE: I structured the code below in a way to make it easy to understand. In a production environment, I would make this code a lot more generic, but I'll leave that for you 😊
Getting The View Model: The first step is to create the view model object. Creating a view model is pretty easy. In my example, I'm using the Episerver API and a page ID to create an instance of a page:
Getting The Controller Name: You can get the controller name you need to call using reflection. Hard coding controller names is not recommended, or scalable 😕. The code to do this is shown below:
The Index Action Name: Episerver works using the default Index()
action. The action name is simply the string value Index
:
The Request Url: This is the Site's Url. You can get this value from the Episerver.Configuration.Settings.Instance
, like so:
We now have all the pieces of the puzzle. We are now ready to write the code to manually call the controller 😊
How To Manually Call A Page Controller in Episerver CMS
To make this magic work, you need a controller context. The controller context object will give you access to everything you need to get this working:
The data required to correctly perform a redirect to another controller is the controller name
, action name
and view model
. With this data, you can create an instance of the controller, creating a new HttpContext
instance. You set the view you want to call using GetPartial()
. Finally, you create a new ViewContext
and call the view render method to manually render the controller. As part of the ViewContext
, you will need to pass in a string builder. In this string builder, the components HTML will be added. You can then use the string builder to add the HTML to the current request context in order to render it. The final code will look like this:
You can then call this method like this:
You now know how to manually render an Episerver page or block via normal MVC. The process is long-winded, however, it is possible. Happy Coding 🤘