Sitecore 8.1. Developer's Notes

I took a two months long hiatus from writing to transition into my new role at work. It’s all good now and I am back with an overdue post about Sitecore 8.1

Routes and the MVC Areas

Sitecore 8.1 has finally added native support for MVC areas. If you’ve used areas before (using this option, for example) you probably were calling AreaRegistration.RegisterAllAreas() during <initialize> and registering your routes in RegisterArea():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Web.Mvc;

namespace Solution.Web.Areas.Site
{
public class SiteAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Site";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{
// Register your MVC routes here
}
}
}

You can now take your own area registration out. Or can you? Here’s how Sitecore changed the InitializeRoutes:

1
2
3
4
5
6
7
8
9
10
11
12
13
protected virtual void RegisterRoutes(RouteCollection routes, PipelineArgs args)
{
RouteCollectionExtensions.MapRoute(routes, MvcSettings.SitecoreRouteName ... );
this.SetDefaultValues(routes, args);

// <-- Pay attention to the order
this.SetRouteHandlers(routes, args);

// <-- This is new in 8.1
this.RegisterAreas();

InitializeRoutes.AddRenderersViewFolderToRazorViewEngine();
}

The problem is - area registration is called after route handlers have been set. I briefly mentioned the custom route handlers in my older blog post about Sitecore MVC routing mechanisms. This is what makes otherwise regular MVC routes Sitecore aware. No custom handlers on your routes means no mvc.* pipelines, no PageContext, no analytics.

All the routes that you were registering in RegisterArea() are now not part of the Sitecore MVC ceremonies. They are late to the party.

Read more