Sitecore MVC Area Controller Rendering Type


I saw this post a couple weeks back about working with Areas in Sitecore MVC. One of the issues was that you had to specify the view path in all of the actions because Sitecore didn’t understand what area you are in. To get around this I created my own rendering type that allows me to specify an Area field. The Area is then added to the Route Values Dictionary. I’m expecting this would also fix other problems with working in areas, like linking. I do however recomend that you strongly type the controller name when entering it as Sitecore is not using the Area field when it resolves the controller. Perhaps that code could be updated as well.

The code is here and the package to install it is here.

There are three classes that I will describe below :

AreaController
This class checks to see if the Rendering Item inherits the Area Controller Template. If it does we Assign the AreaControllerRenderer as the Renderer.

AreaControllerRender

This class passes the Action, Controller and Area to the AreaControllerRunner and returns the html to Sitecore.

AreaControllerRunner

This class executes the controller.  The big change is made here where I pass the Area in the Route Values Colleciton.  The important line to get area support is below.

requestContext.RouteData.DataTokens[“area”] = this.Area;

 

There is also a Sitecore.MVC.SitecoreAreas.config file that causes the code to execute in the GetRenderer Pipeline and a new template ‘Sitecore/Templates/User Defined/Mvc Areas/Area Controller’ that has the template to inherit from your rendering.

I personally think everything should be done in areas as it makes things more portable to other projects.  If you’re not using areas you can also use this project as a sample on how to extend Sitecore to support your own rendering type.

Advertisements