Last active
August 29, 2015 14:19
-
-
Save rbaarda/b2a753f09053411859bb to your computer and use it in GitHub Desktop.
EPiServer default page controller which uses page type name as controller name in view locations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using Example.Models.ViewModels; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.Framework.DataAnnotations; | |
using EPiServer.Web.Mvc; | |
namespace Example.Controllers.Pages | |
{ | |
[TemplateDescriptor(Inherited = true)] | |
public class DefaultPageController : PageController<PageData> | |
{ | |
public ViewResult Index(PageData currentPage) | |
{ | |
IPageViewModel<PageData> model = PageViewModel.CreateModel(currentPage); | |
return DefaultPageViewResult(currentPage.GetOriginalType().Name, model); | |
} | |
private DefaultPageViewResult DefaultPageViewResult(string name, IPageViewModel<PageData> model) | |
{ | |
ViewData.Model = model; | |
return new DefaultPageViewResult(name) | |
{ | |
ViewName = "Index", | |
MasterName = null, | |
ViewData = ViewData, | |
TempData = TempData, | |
ViewEngineCollection = ViewEngineCollection | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
namespace Example.Controllers.Pages | |
{ | |
public class DefaultPageViewResult : ViewResult | |
{ | |
private readonly string name; | |
public DefaultPageViewResult(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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Example.Models.Layout; | |
using EPiServer.Core; | |
namespace Example.Models.ViewModels | |
{ | |
public interface IPageViewModel<out TPage> | |
where TPage : PageData | |
{ | |
TPage CurrentPage { get; } | |
ILayoutModel Layout { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Example.Models.Layout; | |
using EPiServer; | |
using EPiServer.Core; | |
namespace Example.Models.ViewModels | |
{ | |
public class PageViewModel<TPage> : IPageViewModel<TPage> where TPage : PageData | |
{ | |
public TPage CurrentPage { get; private set; } | |
public ILayoutModel Layout { get; set; } | |
public PageViewModel(TPage currentPage) | |
{ | |
CurrentPage = currentPage; | |
} | |
} | |
public static class PageViewModel | |
{ | |
public static IPageViewModel<TPage> CreateModel<TPage>(TPage page) where TPage : PageData | |
{ | |
Type type = typeof(PageViewModel<>).MakeGenericType(page.GetOriginalType()); | |
return Activator.CreateInstance(type, page) as IPageViewModel<TPage>; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment