Code sample
Pattern for EPiServer block preview MVC controller
using System.Web.Mvc; using EPiServer.Core; using EPiServer.Framework.DataAnnotations; using EPiServer.Framework.Web; using EPiServer.Web; namespace MySite { [TemplateDescriptor( Inherited = true, TemplateTypeCategory = TemplateTypeCategories.MvcController, Tags = new [] { RenderingTags.Preview }, AvailableWithoutTag = false)] public class PreviewController : Controller, IRenderTemplate<BlockData> { public ActionResult Index(IContent currentContent) { //TODO: Create model suitable for the view and return ViewResult return View(); } } }
Explained:
using System.Web.Mvc; using EPiServer.Core; using EPiServer.Framework.DataAnnotations; using EPiServer.Framework.Web; using EPiServer.Web; namespace MySite { [TemplateDescriptor( //Support everything inheriting from BlockData. Inherited = true, //By default controllers implementing IRenderTemplate<T> where T is BlockData //are registered as partial renderers. As this will be a "full page" renderer //we need to change that. TemplateTypeCategory = TemplateTypeCategories.MvcController, //Should only be used for preview Tags = new [] { RenderingTags.Preview }, AvailableWithoutTag = false)] public class PreviewController : Controller, //Register as template for BlockData. To only support a specific type //change the type parameter from BlockData to that type and optionally //set Inherited = false in the the TemplateDescriptor attribute above. IRenderTemplate<BlockData> { public ActionResult Index( //While we implement IRenderTemplate for BlockData model binding //can only deal with IContent, ie shared blocks in this case. IContent currentContent ) { //TODO: Create model suitable for the view and return ViewResult return View(); } } }