# MVC areas

Abstract

Areas allow grouping collections of controllers and views to help subdivide and organize complex projects .

Areas enable you to group collections of controllers and views together to help subdivide and organize complex projects. This is helpful when you have large teams of MVC developers. Sitecore can use an area for each rendering, including the main view layout.

You can specify the area in several ways:

• In the rendering or layout definition item

• In the rendering parameters

• When invoking a rendering in code, for example: @Html.Sitecore().ViewRendering("Index", new { area = "MyArea" })

• In the route (although the default configuration disables the PassthroughAreaResolveStrategy on which this feature depends)

You can add your own area resolution logic. For example, you can determine the area from an ancestor or folder that specifies an area, or from an attribute of the context site, for example the/configuration/sitecore/sites/site element in the Web.config file.

To register areas, Sitecore uses the InitializeRoutes processor in the Initialize pipeline. This processor is responsible for configuring MVC routes, and it invokes its RegisterAreas() method. This method scans all assemblies for types that it can instantiate as System.Web.Mvc.AreaRegistration objects. For these objects, the RegisterAreas() method creates an instance of the System.Web.Mvc.AreaRegistrationContext class and passes that to the RegisterArea() method of the instance.

You can create an area by right-clicking a project in Visual Studio, clicking Add, and then selecting Area.

This is an example of how to register and create areas:

public class FirstAreaRegistration : System.Web.Mvc.AreaRegistration
{
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute("FirstContainer", "First/{controller}/{action}/{id}",
new {controller = "General", action = "Test", id = UrlParameter.Optional});
}
public override string AreaName => "First";
}
public class SecondAreaRegistration : System.Web.Mvc.AreaRegistration
{
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute("SecondContainer", "Second/{controller}/{action}/{id}",
new {controller = "General", action = "Test", id = UrlParameter.Optional});
}
public override string AreaName => "Second";
}

### Note

You must add namespaces into the context.Namespaces collection.

To determine the area for each rendering and layout view, Sitecore uses the ResolveArea processor early in the mvc.renderRendering pipeline, immediately after initializing profiling for the rendering. The only constructor for the ResolveArea processor requires an instance of IAreaResolveStrategy. The default configuration applies the ChainedAreaResolveStrategy, which invokes a series of IAreaResolveStrategy instances until one returns a value or there are none left.

The default series of the IAreaResolveStrategy types contains:

• RenderingDefinitionAreaResolveStrategy – determines the area from the rendering definition item. To use this approach, specify the area in the Area field in the Data section of the definition item for the controller or view renderings.

• RenderingParametersAreaResolveStrategy – determines the area from the rendering parameter called area. To use this approach, specify the area in a rendering parameter, for example area = "MyArea".

• RenderingLayoutAreaResolveStrategy – determines the area from the layout definition item for layout views To use this approach, specify the area in the Area field in the Data section of the definition item for the MVC view layout.

The /App_Config/Include/Sitecore.Mvc.config file defines the /configuration/sitecore/mvc element. This element instructs the InitializeRoutes processor to exclude areas that the new area logic could interfere with.

<mvc.renderRendering>
<processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.InitializeProfiling, Sitecore.Mvc"/>
<processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.ResolveArea, Sitecore.Mvc">
<param desc="areaResolver" type="Sitecore.Mvc.Pipelines.Response.RenderRendering.ChainedAreaResolveStrategy, Sitecore.Mvc">
<Resolvers hint="list">
<resolver type="Sitecore.Mvc.Pipelines.Response.RenderRendering.RenderingDefinitionAreaResolveStrategy, Sitecore.Mvc"/>
<resolver type="Sitecore.Mvc.Pipelines.Response.RenderRendering.RenderingParametersAreaResolveStrategy, Sitecore.Mvc"/>
<resolver type="Sitecore.Mvc.Pipelines.Response.RenderRendering.RenderingLayoutAreaResolveStrategy, Sitecore.Mvc"/>
</Resolvers>
</param>
</processor>
<processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.SetCacheability, Sitecore.Mvc"/>


The main extensibility point is the IAreaResolveStrategy. You use this to provide strategies for resolving area. Sitecore comes with a number of strategies by default:

• ChainedAreaResolveStrategy – resolves the area information by walking over a list of IAreaResolveStrategy objects. First one to answer with a non-null answer wins. If none of the strategies in the list resolves an area, the Resolve method returns null.

• RenderingDefinitionAreaResolveStrategy – resolves the area by looking for an Area field in the current rendering definition item.

• RenderingParametersAreaResolveStrategy – resolves the area by looking for an area key in the parameters dictionary. This can be specified for statically bound View Renderings by adding an anonymous object parameter, for example:

@Html.Sitecore().ViewRendering("Index", new { area = "MyArea" })

• RenderingLayoutAreaResolveStrategy – resolves the area by looking for an Area field the current layout definition item.