When working with Episerver, you get very used to rendering pages and blocks, however, if you want to render a normal MVC controller, life can be a little more difficult without the right know how.

Routing

Before we go into the code, we need to talk about routing. If you want to access a normal controller within an Episerver site, you need to configure a custom route in your route table.

This step is slightly different to a normal C# MVC website. In a bog standard MVC site, the sites Url structure usually maps directly to the controllers and actions defined, e.g. www.website.com/home would map a controller called home.

In Episerver world though, this routing structure won’t work. In Episerver you have pages which are generated via page types. A single page type will have a single controller. One page type definition will be used to generate multiple pages, e.g. a content page type may be used to create 10 or 20 different pages within the site so EpiSever can’t match a Url to a controller.

Behind the scenes a different type of route is used. When you set-up an Episerver site via Nuget, a custom Episerver route is included that maps an incoming Url request to point to the correct Episerver page controller. I won’t go into the details but just trust that it happens.

This means that if you want to start working with a normal controller, the first thing we need to do to register a route that maps a url to the controller you want to trigger:

In the code above, I’m creating a Route definition with a name called ‘DebuggingInformation’. This identifier can be anything you like as it’s an internal name only which means it’s never used in the front-end. This name is really only used for debugging.

The second part of the route definition defines the Url that will trigger the controller to be called. In this example a Url with www.website.com/DebuggingInformation will trigger the route.

The last part maps which controller and index to use. In this case we will create a controller called MetaDataController (notice how you don’t need to use the Controller part in the route) with a default Index action.

The controller inherits form ‘Controller’ we don’t use any Pages or Block at all 🙂 In case you still want to get access to Episerver data you can use the PageRouteHelper to get information. In this example, this page will get information about the start page. (If you are wondering why we would need this in a non Episerver page, then in my next guide I’ll show you how you can create custom views into to a pluggable area)

After we do all the processing we need, we can then render a view as normal.

The View

Lastly, we create a MetaData folder in underneath the view folder and create a Razor page called index.cshtml. Within the view I’m using the following mark-up:

The most important part within the view is setting the Layout to use an empty string. If you don’t do this, IIS will try to load your normal layout and the site will likely throw an exception because it doesn’t have the same data within the HTTP Context when using ‘Controller’ as you do in ‘PageController’.

Conclusion

In today’s lesson, we’ve covered how to use a normal page controller in Episerver. First you need to define a route that will point to your controller and action. Next you create a controller that inherits from ‘Controller’, lastly you create a view making sure you override the Layout property so the main sites header and footer doesn’t load.

Code Sample

All the above code can be downloaded in a fully working website from my github account here.

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

NEED AN EPISERVER DEVELOPER?

Connect

Hi, I'm Jon, I write articles about creating and optimizing websites as well as making money from your online business. I am technical architect and technology fanatic by profession. You can find out more about me by signing up to my newsletter.