Skip to content

Instantly share code, notes, and snippets.

@rbaarda
Created April 24, 2015 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbaarda/ae23e5909fa2889cc288 to your computer and use it in GitHub Desktop.
Save rbaarda/ae23e5909fa2889cc288 to your computer and use it in GitHub Desktop.
EPiServer default block controller plus block preview controller which uses block type name as controller name in view location.
using System;
using EPiServer;
using EPiServer.Core;
namespace Example.Models.ViewModels
{
public class BlockViewModel<TBlock> : IBlockViewModel<TBlock> where TBlock : BlockData
{
public TBlock CurrentBlock { get; private set; }
public BlockViewModel(TBlock currentBlock)
{
CurrentBlock = currentBlock;
}
}
public static class BlockViewModel
{
public static IBlockViewModel<TBlock> CreateModel<TBlock>(TBlock block) where TBlock : BlockData
{
Type type = typeof(BlockViewModel<>).MakeGenericType(block.GetOriginalType());
return Activator.CreateInstance(type, block) as IBlockViewModel<TBlock>;
}
}
}
using System.Web.Mvc;
using Example.Models.ViewModels;
using EPiServer;
using EPiServer.Core;
using EPiServer.Web.Mvc;
namespace Example.Controllers.Blocks
{
public class DefaultBlockController : BlockController<BlockData>
{
public override ActionResult Index(BlockData currentBlock)
{
IBlockViewModel<BlockData> model = BlockViewModel.CreateModel(currentBlock);
return DefaultBlockViewResult(currentBlock.GetOriginalType().Name, model);
}
private DefaultBlockViewResult DefaultBlockViewResult(string name, IBlockViewModel<BlockData> model)
{
ViewData.Model = model;
return new DefaultBlockViewResult(name)
{
ViewName = "Index",
ViewData = ViewData,
TempData = TempData,
ViewEngineCollection = ViewEngineCollection
};
}
}
}
using System.Web.Mvc;
using Example.Controllers.Pages;
using Example.Models.ViewModels;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.DataAnnotations;
using EPiServer.Framework.Web;
using EPiServer.Web;
namespace Example.Controllers.Blocks.Preview
{
/// <summary>
/// Based on: http://joelabrahamsson.com/pattern-for-episerver-block-preview-mvc-controller/
/// </summary>
[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 DefaultBlockPreviewController : 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
)
{
IBlockViewModel<BlockData> model = BlockViewModel.CreateModel((BlockData)currentContent);
return DefaultPageViewResult(currentContent.GetOriginalType().Name, model);
}
private DefaultPageViewResult DefaultPageViewResult(string name, IBlockViewModel<BlockData> model)
{
ViewData.Model = model;
return new DefaultPageViewResult(name)
{
ViewName = "Index",
MasterName = null,
ViewData = ViewData,
TempData = TempData,
ViewEngineCollection = ViewEngineCollection
};
}
}
}
using System.Web.Mvc;
namespace Example.Controllers.Blocks
{
public class DefaultBlockViewResult : PartialViewResult
{
private readonly string name;
public DefaultBlockViewResult(string name)
{
this.name = name;
}
protected override ViewEngineResult FindView(ControllerContext context)
{
context.RouteData.Values["controller"] = name;
context.RouteData.Values["action"] = "Index";
return base.FindView(context);
}
}
}
using EPiServer.Core;
namespace Example.Models.ViewModels
{
public interface IBlockViewModel<out TBlock> where TBlock : BlockData
{
TBlock CurrentBlock { get; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment