MVC improvements, area support, and MVC 5.2.3 in Sitecore 8.1

By: Sitecore MVP Glen McInnis

Valtech
Valtech — Sitecore experts since 2008
3 min readJun 17, 2016

--

Sitecore 8.1 includes a lot of new features for content authors and marketers, but developers need not fear being left out in the cold. With Sitecore 8.1 comes an MVC upgrade from 5.1 to 5.2.3, as well as official support for MVC Areas.

Let’s start with the MVC 5.2.3 upgrade

The upgrade is welcome for a number of reasons, but the improvement in Razor performance is personally top of mind. While not strictly MVC-related, Sitecore has also invested some time in lowering the startup time of Sitecore 8.1. I’ve not had a chance to benchmark 8.1, but I am looking forward to the faster experience.

On to areas

Sitecore has introduced area support for Layouts, Controller Renderings and View Renderings. Some of the hangouts and notes indicate that more may be coming, but this does provide a solid starting place and should stop the continuing sprawl of community-grown solutions for Area support.

So, how does this all work?

Area registration

Area registration is done during the initialize pipeline. If you do not want your area to be registered, you can add it to the exclusion list in the patch files at /sitecore/mvc/areas/excludeAreas. The following is fromApp_Config/Include/Sitecore.Mvc.Config

Area resolution

Area resolution is performed by the new processor ResolveArea. As you can see in the configuration below, a first to resolve chained strategy is used to resolve areas. It starts with the rendering item, then proceeds to the rendering parameters and finally the layout.

For rendering or layout item resolution, the configuration is quite simple. A new Area field has been added on the Layout, Controller Rendering and View Rendering definition. Simply set a value in the field. Please note that this is a unique field on each of the templates used to define Layout, Controller and View items. If you need to access the field, do so by name and not GUID.

For parameter resolution, add the area name to the standard values, or add the parameter in code.

@Html.Sitecore().Rendering(“/sitecore/layout/Renderings/Rendering Folder/View rendering”, new { area = “Keystone” })

Custom area resolution

You can of course plugin your own area resolution logic. Your class must implementSitecore.Mvc.Pipelines.Response.RenderRendering.IAreaResolveStrategy. The method executed as part of the processor is Resolve.

public class KeystoneSiteAreaResolveStrategy : IAreaResolveStrategy
{
public string Resolve(RenderRenderingArgs args)
{
var property = GetSiteArea();
return string.IsNullOrWhiteSpace(property) ? null : property;
}

public string GetSiteArea()
{
// sub-sections or microsites may have different areas
if(Sitecore.Context.IsMicrosite())
return GetMicroSiteArea()
else
return Sitecore.Context.Site.Properties[“area”];
}
}

Looking for more Sitecore insight? Visit nonlinearcreations.com

--

--

Valtech
Valtech — Sitecore experts since 2008

Valtech is a full-service digital agency. Our staff of 2,500 operates from 36 offices around the world.