an abstract image of a grid adhering to waves with various spotlights of teal and purple

Sitecore MVC Areas

Ben Golden
Ben Golden

Check out this Sitecore MVC extension that adds support for Areas and dependency injection with controller renderings that use fully-qualified type names.

When developing our internal standards around Sitecore MVC implementations, one of the first things we wanted was a way to keep our solution code contained and organized. In ASP.NET MVC, the convention for this is to use Areas. Unfortunately, Sitecore MVC does not yet fully support Areas out-of-the-box.

A couple of years ago, Kevin Obee showed how you can use Areas with Sitecore MVC. However, it can lead to naming conflicts between controllers and requires that you specify the full path to your views. Kevin Buckley noted that you can use fully-qualified type names to specify your controllers and, a couple of weeks later, provided new rendering type that removed the need to specify the full path for views.

There was really just one problem with Kevin’s extension. If you are using fully-qualified type names to specify your controllers, the SitecoreControllerFactory will use plain old reflection to create an instance. It does not invoke your dependency injection container. For those of us that are sticklers for best practices and SOLID design principles, this is a big problem.

As usual with Sitecore, fixing the issue is pretty easy. There are two changes that need to be made.

First, we need a controller factory that will use the DependencyResolver to create the controller instance if the controller name looks like a fully-qualified type name.  I created a FullyQualifiedSitecoreControllerFactory to do just that. This controller factory can be used instead of the default SitecoreControllerFactory by replacing the InitializeControllerFactory processor in the initialize pipeline with a different implementation.

The second problem we need to fix is that the ControllerRunner is also using reflection to create a controller instance. It should really just get out of the way and let the configured controller factory handle creating the controller instance. I modified the AreaControllerRunner that Kevin Buckley provided to do just that.

I combined the extensions above with a somewhat refactored version of Kevin Buckley’s code and made it available on GitHub. A Sitecore package is also available. Enjoy!

Let's Get Started

We'd love to hear from you. We probably have a lot in common. I mean, you like chatting about data-binding, UX patterns, and javascript functions, right?


Cookies help us improve your website experience. By using our website, you agree to our use of cookies and our privacy policy.