Take the community feedback survey now.


Jan 9, 2013
  19767
(0 votes)

Building EPiServer Templates with MVC Razor Part 1: Page Templates

EPiServer 7 CMS provides full support for creating templates with MVC using the Razor and Web Forms view engines.  This is a step by step how-to guide to help you get started creating EPiServer Page Templates and Block Templates using MVC with the Razor view engine.  Part 1 of this series describes how to create Page Templates and Part 2 describes how to build Block Templates.

 

Prerequisites

This blog entry assumes you have a basic understanding of  EPiServer 7 CMS from a user and developer point of view and are familiar the Microsoft MVC Razor implementation. For more details on using EPiServer, see EPiServer WebHelp.  For more details on Microsoft MVC, see the following articles:

To complete this example, you will need to install EPiServer 7 CMS, prerequisites, and a supported version of Visual Studio with the EPiServer 7 CMS Visual Studio Integration Extension as described in the documentation section of EPiServer World.

 

Create a New EPiServer Project

The first step is to create a new EPiServer web site project in Visual Studio using the EPiServer project template.

  1. File > New Project… > Installed > Templates > Visual C# > EPiServer > EPiServer Web Site (MVC)

 

image

 

The EPiServer Deployment Center executes Windows PowerShell Scripts that perform the following:

  1. Creates the Visual Studio project
  2. Copies in the necessary EPiServer binaries
  3. Sets up the configuration files
  4. Creates the EPiServer content database
  5. Sets up IIS Express to host the web site

 

image

Create the Standard Page Model / Page Type

Next, we need to create a Model which defines the properties and contains content for pages of the StandardPage Page Type.  In EPiServer, a Page Type defines the properties for a specific type of page. In this case, the Page Type is assuming the role of the Model.

  1. Models > Pages > Add > New Item... > Installed > Visual C# > EPiServer > Page Type > StandardPage.cs
  2. Uncomment the MainBody property
using System;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.SpecializedProperties;

namespace EPiServerMvcSite1.Models.Pages
{
[ContentType(DisplayName = "StandardPage", GUID = "36186065-ebac-404b-96b7-6db10849a98a", Description = "")]
public class StandardPage : PageData
{
[CultureSpecific]
[Editable(true)]
[Display(
Name = "Main body",
Description = "The main body will be shown in the main content area of the page, using the XHTML-editor you can insert for example text, images and tables.",
GroupName = SystemTabNames.Content,
Order = 1)]
public virtual XhtmlString MainBody { get; set; }
}
}

Note that the class inherits from EPiServer.Core.PageData which is an object representation of the content.  The ContentType class attribute registers the Page Type in EPiServer so no additional configuration is required for deployment.  Properties are defined in code using the EPiServer property types in EPiServer.Core.  The Display property attribute enables you to specify metadata for the property including the display name, sort order, and which tab the property appears in the EPiServer editorial interface.

 

Create the Standard Page Controller

The Controller receives the request for a page from the browser, retrieves the Model (content) for the requested page and passes the Model to the View via an action method for rendering to the browser.  EPiServer invokes different controller classes (and different action methods within them) depending on the Page Type of the requested page and the incoming URL. 

  1. Controllers > Add > New Item… > Installed > Visual C# > EPiServer > Page Controller (MVC) > StandardContoller.cs
  2. Resolve StandardPage using <ProjectName>.Models.Pages 
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Web.Mvc;
using EPiServerMvcSite1.Models.Pages;

namespace EPiServerMvcSite1.Controllers
{
public class StandardController : PageController<StandardPage>
{
public ActionResult Index(StandardPage currentPage)
{
/* Implementation of action. You can create your own view model class that you pass to the view or
* you can pass the page type for simpler templates */

return View(currentPage);
}
}
}

Note that PageController<StandardPage> instructs EPiServer to invoke this controller for pages of the StandardPage Page Type. 

 

Create the Standard Page View

A View renders the content in the Model that is passed to it by the Controller.

  1. Create a Standard folder in Views
  2. Views > Standard > Add > New Item… > Installed > Visual C# > EPiServer > Page Partial View (MVC Razor) > Index.cshtml
  3. Update the @model statement to: <ProjectName>.Models.Pages.StandardPage.

Note that the @model statement gives you strongly typed access to the Model.

Helper Methods

The Html.PropertyFor() helper method renders a property and is analogous to the EPiServer:Property control in Web Forms.  EPiServer provides a number of helper methods for rendering content which are described in the EPiServer SDK.

 

Create the Home Page

  1. Build & run the project
  2. Navigate to http://localhost:<port>/EPiServer/CMS
  3. Login with an account that is a local Administrator
  4. Create the Home page using the StandardPage Page Type
  5. Add text to the Main Body property
  6. Publish and view the page

 

image

 

Wire Up the Sample Layout

A layout in MVC is analogous to a Master Page in web forms. A layout can contain Sections that are analogous to ContentPlaceHolders and a View can have Section Overrides that are analogous to Content controls.

  1. Copy the Content folder(contains Site.css) from the supporting files to the root of project
  2. Copy Views/Shared/_Layout.cshtml to Views/Shared
  3. Copy Views/_ViewStart.cshtml to Views
  4. Refresh the browser

 

image

_Layout.cshtml

@using EPiServer.Web.Mvc.Html
@using EPiServer.Core

<!DOCTYPE html>
<html lang="@Html.ViewContext.RequestContext.RouteData.Values["lang"]">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="generator" content="EPiServer CMS Falcon" />
<meta name="description" content="" />
<meta name="keywords" content=""/>
<title>EPiServer MVC Razor Example</title>
<link rel="stylesheet" href="@Href(Url.Content("~/Content/Site.css"))" media="all"/>
</head>
<body>
<header>
<h1>EPiServer MVC Razor Example</h1>
<section>
</section>
<nav id="topnavigation">
@RenderSection("TopNav", required:false)
</nav>
</header>
<section id="main">
<nav id="subnavigation">
</nav>
<section id="content">
@RenderBody()
</section>
</section>
<footer class="mainfooter">
<p>&copy; Copyright @DateTime.Now.Year - EPiServer, Inc.</p>
</footer>
</body>
</html>

_ViewStart.cshtml

This file is used to declare common properties that your Views use. It instructs which layout to use to render the page.

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}



 

Supporting Files

The file below includes the CSS and layout files referenced in this guide.

Stay tuned for Part 2 of this series where we will build a Block Template to enable editors to create blocks of content that can be reused across pages, sites, and channels.

Jan 09, 2013

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP - Opticon London 2025

This installment of a day in the life of an Optimizely OMVP gives an in-depth coverage of my trip down to London to attend Opticon London 2025 held...

Graham Carr | Oct 2, 2025

Optimizely Web Experimentation Using Real-Time Segments: A Step-by-Step Guide

  Introduction Personalization has become de facto standard for any digital channel to improve the user's engagement KPI’s.  Personalization uses...

Ratish | Oct 1, 2025 |

Trigger DXP Warmup Locally to Catch Bugs & Performance Issues Early

Here’s our documentation on warmup in DXP : 🔗 https://docs.developers.optimizely.com/digital-experience-platform/docs/warming-up-sites What I didn...

dada | Sep 29, 2025

Creating Opal Tools for Stott Robots Handler

This summer, the Netcel Development team and I took part in Optimizely’s Opal Hackathon. The challenge from Optimizely was to extend Opal’s abiliti...

Mark Stott | Sep 28, 2025

Integrating Commerce Search v3 (Vertex AI) with Optimizely Configured Commerce

Introduction This blog provides a technical guide for integrating Commerce Search v3, which leverages Google Cloud's Vertex AI Search, into an...

Vaibhav | Sep 27, 2025

A day in the life of an Optimizely MVP - Opti Graph Extensions add-on v1.0.0 released

I am pleased to announce that the official v1.0.0 of the Opti Graph Extensions add-on has now been released and is generally available. Refer to my...

Graham Carr | Sep 25, 2025