Last active
August 29, 2015 14:01
-
-
Save jamiehowarth0/f0d7b93d42e85ac4593b to your computer and use it in GitHub Desktop.
How to use uMapper to return strong POCO types in Umbraco MVC mode
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 System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Umbraco.Core; | |
using Umbraco.Web.Mvc; | |
using UmbracoStrongTypeViews.Controllers; | |
namespace UmbracoStrongTypeViews.App_Start { | |
public class ApplicationStartupHandler : IApplicationEventHandler { | |
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { | |
} | |
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { | |
DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(BassFunkyController)); | |
} | |
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { | |
} | |
} | |
} |
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 System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using uComponents.Mapping; | |
using umbraco.NodeFactory; | |
using Umbraco.Web.Mvc; | |
namespace UmbracoStrongTypeViews.Controllers { | |
public class BassFunkyController : RenderMvcController { | |
public override System.Web.Mvc.ActionResult Index(Umbraco.Web.Models.RenderModel model) { | |
try { | |
var hasMapping = uMapper.Engine.NodeMappers; | |
if (hasMapping.Any(p => p.Value.SourceDocumentType.Alias == model.Content.DocumentTypeAlias)) { | |
// We're able to map current node type to a POCO. | |
Type resultType = hasMapping.First(p => p.Value.SourceDocumentType.Alias == model.Content.DocumentTypeAlias).Value.DestinationType; | |
var typeConversion = typeof(uMapper).GetMethod("Map").MakeGenericMethod(resultType); | |
var rawObject = typeConversion.Invoke(resultType, new object[] { new Node(model.Content.Id), false }); | |
return CurrentTemplate(rawObject); | |
} else { | |
// No POCO model, so return Rendermodel | |
return base.Index(model); | |
} | |
} catch (InvalidOperationException NoObjectException) { | |
// Exception, return base RenderModel | |
return base.Index(model); | |
} | |
} | |
} | |
} |
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 System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace UmbracoStrongTypeViews.Models { | |
public class Homepage { | |
public string Title { get; set; } | |
public string BodyText { 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
@model Homepage | |
@{ | |
ViewBag.Title = "Homepage"; | |
} | |
<h2>@Model.Title</h2> | |
@Html.Raw(Model.BodyText) |
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
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<configSections> | |
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> | |
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> | |
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> | |
</sectionGroup> | |
</configSections> | |
<system.web.webPages.razor> | |
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> | |
<pages pageBaseType="Umbraco.Web.Mvc.UmbracoViewPage"> | |
<namespaces> | |
<add namespace="System.Web.Mvc" /> | |
<add namespace="System.Web.Mvc.Ajax" /> | |
<add namespace="System.Web.Mvc.Html" /> | |
<add namespace="System.Web.Routing" /> | |
<add namespace="Umbraco.Web" /><add namespace="Umbraco.Core" /><add namespace="Umbraco.Core.Models" /><add namespace="Umbraco.Web.Mvc" /><add namespace="Microsoft.Web.Helpers" /><add namespace="umbraco" /><add namespace="Examine" /></namespaces> | |
</pages> | |
</system.web.webPages.razor> | |
<appSettings> | |
<add key="webpages:Enabled" value="false" /> | |
</appSettings> | |
<system.web> | |
<httpHandlers> | |
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler" /> | |
</httpHandlers> | |
<!-- | |
Enabling request validation in view pages would cause validation to occur | |
after the input has already been processed by the controller. By default | |
MVC performs request validation before a controller processes the input. | |
To change this behavior apply the ValidateInputAttribute to a | |
controller or action. | |
--> | |
<!-- | |
Enabling request validation in view pages would cause validation to occur | |
after the input has already been processed by the controller. By default | |
MVC performs request validation before a controller processes the input. | |
To change this behavior apply the ValidateInputAttribute to a | |
controller or action. | |
--><pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> | |
<controls> | |
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" /> | |
</controls> | |
</pages> | |
</system.web> | |
<system.webServer> | |
<validation validateIntegratedModeConfiguration="false" /> | |
<handlers> | |
<remove name="BlockViewHandler" /> | |
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> | |
</handlers> | |
</system.webServer> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note generic type Umbraco.Web.Mvc.UmbracoViewPage is the base page type in Views/web.config.
By default this expects T to be a RenderModel, but in all honesty it can be absolutely anything you like - hence our ability to dynamically dispatch uMapper to return a POCO representation of our model via overriding Index() to return CurrentTemplate() instead.