This post is another code example that the majority of people will never really need. If you are reading this and you think you want to use the demo code, then my advice would be to strongly consider if the design of your project is correct. In almost every situation, it's a lot easier to let Episerver and the Episerver API do the hard work for you rather than do it manually. If you are interested in roughly how Episerver hi-jacks the MVC pipeline and renders virtual pages then the code is definitely worth giving a once over.

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, which include the Episerver page, a view model (I assume you use them!), the controller to use, the action to invoke and the site's Url. In my example below, I'm manually rendering content Pages' which is a custom page type that I've defined myself.

The Page Type

The content page type definition class, looks like this:

The View Model

The view model class, looks like this:

The Controller

The page controller class, looks like this:

Now we have the basics defined for the content page, we will need the following bits of data in order to manually render it:

  • An Instance Of a Page
  • The View Model
  • The Name Of The Content Pages Controller
  • The Action Within The Controller To Invoke

NOTE: I wrote 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

Creating the 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

Hard coding names are bad. Instead, we can use reflection on the content page controller to get its name

The Index Name

This part is easy, I just hard code in the 'Index' string:

The Request Url

This is just the Site's Url, which we can get from the EPiServer.Configuration.Settings.Instance, like so:

Now we have all the pieces of the puzzle, we can now write some code to actually manually render the HTML we want:

First, we create a new controller context. When we work with MVC the controller context is the basic building block for any MVC controller. In here we define the route data (controller name, action and view model), an instance of the controller we want to call and a new HttpContext instance. We set the view we want to call, by calling GetPArtial(). Then we create a new View Context and call the view render method to manually call it. As part of the view context, we pass in a string builder. In this string builder, all the rendered HTML of the controller will be rendered.

The Final Code

You can then call the code directly like this:

Today's post was slightly different than normal. In this post, I've hopefully demonstrated how to manually render an Episerver page or block via normal MVC.