November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
You can create page templates in EPiServer, using ASP.NET Web Forms or MVC; each is fully valid.
Create a page template in Visual Studio as follows:
If you use a strongly-typed page type template, the template is automatically registered as a supported template for the specified page type (T). To make the template supported for all page types in the system, use EPiServer.Core.PageData as the generic type (T).
Use the [TemplateDescriptor] attribute on templates to add metadata to the template. You also can use the attribute to set the template as the default template for the page data. See the Attributes article for information about the TemplateDescriptor attribute.
You can use several [TemplateDescriptor] attributes on the same template to specify several page types that have the template as a supported renderer. When the template is strongly typed, you must derive the ModelType type from the generic type. If two page types use one template, you can use the multi-attribute functionality by setting the generic type to PageData and add two [TemplateDescriptor] attributes, which specifies the types in the ModelType property.
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAnnotations;
using EPiServer.Framework.DataAnnotations;
using EPiServer.SpecializedProperties;
using EPiServer.Web;
[ContentType]
public class MyPage : PageData
{
public virtual XhtmlString MainBody { get; set; }
public virtual PageList MainList { get; set; }
}
[TemplateDescriptor(Name = "My template", Description = "My first template", Path = "~/templates/MyTemplate.aspx", Default = true)]
public partial class MyTemplate : TemplatePage<MyPage>
{ }
Create a block template in Visual Studio as follows:
The template is automatically registered as a supported template for the specified block type (T). Use the EPiServer.Core.BlockData as the generic type (T) to make the template supported for all block types in the system.
Use the [TemplateDescriptor] attribute, which is in the EPiServer.Framework.DataAnnotations namespace, on controls to add meta data to the template. You also can us the attribute to set the control as the default template for the block data. The attribute contains the following properties:
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAnnotations;
using EPiServer.Framework.DataAnnotations;
using EPiServer.SpecializedProperties;
using EPiServer.Web;
[ContentType(DisplayName = "A page list", Description = "A block of properties needed to display a page list")]
public class PageList : BlockData
{
public virtual PageReference Root { get; set; }
public virtual int Count { get; set; }
public virtual string Heading { get; set; }
}
[TemplateDescriptor(Name = "My block control", Description = "My first block control", Path = "~/templates/MyBlockControl.ascx", Default = true)]
public partial class MyBlockControl : BlockControlBase<PageList>
{ }
To render an MVC view for a page type, create a controller that inherits from EPiServer.Web.Mvc.PageController<T>, where T is your page type. This controller is called for the page type, if it is chosen as the renderer for the page type. To render EPiServer CMS properties, use the Html.PropertyFor extension method, which calls another view that has the same name as the property type you are about to render. EPiServer has views for built-in properties that render the properties, as shown in the following example.
public class AcmeStandardPageController : PageController<MyStandardPage>
{
public ActionResult Index()
{
return View();
}
}
[ContentType]
public class MyStandardPage : PageData
{
public virtual string MyText { get; set; }
}
You can use the HTML helpers in the EPiServer.Web.Mvc.Html namespace as an alternative to Html.PropertyFor. The HTML helpers are called through Html.DisplayFor, but you can use them directly.
You can make a dynamic content plug-in support MVC in the following ways:
For the DynamicPageProperty plug-in, the name is DynamicPageProperty.ascx or DynamicPageProperty.cshtml. The view takes the dynamic content plug-in as the view model, and renders the dynamic content in view mode. The following example shows a simple dynamic content (DdsViewerDynamicContent), and a razor view for rendering the content.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.DynamicContent;
using EPiServer.PlugIn;
using EPiServer.Data.Dynamic;
using EPiServer.Core;
namespace CodeSamples.Additional_Content.HowTo
{
/// <summary>
/// Dynamic content showing the first 100 items in a Dynamic Data Store.
/// A specific editor is used to select store.
/// </summary>
[GuiPlugIn(Url = "~/EPiServer/DynamicContent/DdsViewerDynamicContentEdit.ascx", Area = PlugInArea.DynamicContent)]
public class DdsViewerDynamicContentUsingRazor : IDynamicContentBase
{
/// <summary>
/// Gets and sets the selected store
/// </summary>
public string State
{
get;
set;
}
/// <summary>
/// This property is not used, because a specific editor is used.
/// </summary>
public PropertyDataCollection Properties
{
get { return null; }
}
/// <summary>
/// Gets the first 100 hits from the selected store
/// </summary>
public IEnumerable<PropertyBag> Top100
{
get
{
return DynamicDataStoreFactory.Instance.GetStore(State).ItemsAsPropertyBag().Take(100).ToList();
}
}
}
}
@using CodeSamples.Additional_Content.HowTo
@model DdsViewerDynamicContent
<h2>Store: @Model.State</h2>
<table>
@if (@Model.Top100.Count() > 0) {
<tr>
@foreach (string column in @Model.Top100.First().Keys)
{
<th>@column</th>
}
</tr>
}
@foreach (var propertyBag in @Model.Top100)
{
<tr>
@foreach (var value in propertyBag.Values) {
<td>@value</td>
}
</tr>
}
</table>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.DynamicContent;
using EPiServer.PlugIn;
using EPiServer.Data.Dynamic;
using System.IO;
using System.Web.Mvc;
using EPiServer.Core;
namespace CodeSamples.Additional_Content.HowTo
{
/// <summary>
/// Dynamic content showing the first 100 items in a Dynamic Data Store.
/// A specific editor is used to select store.
/// </summary>
[GuiPlugIn(Url = "~/EPiServer/DynamicContent/DdsViewerDynamicContentEdit.ascx", Area = PlugInArea.DynamicContent)]
public class DdsViewerDynamicContentUsingIView : IDynamicContentBase, System.Web.Mvc.IView
{
/// <summary>
/// Gets and sets the selected store
/// </summary>
public string State
{
get;
set;
}
/// <summary>
/// This property is not used, because a specific editor is used.
/// </summary>
public PropertyDataCollection Properties
{
get { return null; }
}
/// <summary>
/// Gets the first 100 hits from the selected store
/// </summary>
public IEnumerable<PropertyBag> Top100
{
get
{
return DynamicDataStoreFactory.Instance.GetStore(State).ItemsAsPropertyBag().Take(100).ToList();
}
}
/// <summary>
/// Renders the data received from the selected store
/// </summary>
/// <param name="context">The context.</param>
/// <param name="writer">The writer.</param>
public void Render(ViewContext context, TextWriter writer)
{
foreach (PropertyBag propertyBag in Top100)
{
foreach (var value in propertyBag)
{
writer.Write(value);
}
}
}
}
}
If there is a display template with the same name as the dynamic content plug-in, and the plug-in implements IView, the system calls the display template and ignores the IView implementation.
To render an XForm property on a page, use the Property and RenderPropertyXForm HTML helper methods in the view. The Property method uses the default behavior, while the RenderPropertyXForm takes the name of the property, and an XFormParameters class as parameters. Optionally use the XFormParameters parameter to override the default settings.
If you do not set the SuccessAction and FailedAction properties on the XFormParameters class, the system uses default views for successful and failed posts. If you set SuccessAction to Success and FailedAction to Failed, the system calls the Success or Failed action in the controller, depending on whether a validation error occured when posting the form. You must create the Success and Failed methods in the controller to make it work.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="EPiServer.XForms.Util" %>
<section id="form">
<%--<% Html.EnableClientValidation(); %>--%>
<%: Html.ValidationSummary() %>
<% Html.RenderPropertyXForm("XForm", new XFormParameters() { SuccessAction = "Success", FailedAction = "Failed" }); %>
<%--<%: Html.Property("XForm") %> --%>
</section>
For EPiServer CMS to match a requested URL to a controller, the EPiServer.Web.HierarchicalUrlRewriteProvider class must be in use, which is the default URL rewrite provider in a newly installed site. For an upgrade, you must change the configuration manually.
You can render an MVC view for a block type in the following ways:
To render EPiServer CMS properties, use the procedure as you do on pages described in this topic.
To require rendering of certain client resources on the page in a specific area, see the requirements for templates to support this in Managing client resources in EPiServer CMS.
Last updated: Feb 23, 2015