Nowadays everyone’s using ASP.NET MVC for Sitecore development, but it could be easier:
- Do you find yourself having to put the fully qualified name of your Controllers into Sitecore?
- Do you find yourself creating variables or constants with full paths to your Views in your Controllers?
- Are your Renderings named after your Controllers and your Views? Ever wish Sitecore would “just get it” instead of having to wire everything up in excruciating detail?
- Wish you could squeeze just a little more performance out of your pages?
- Ever have issues where you couldn’t cache a rendering because it shared a Datasource or Controller with another Rendering?
Constellation.Foundation.Mvc fixes these issues and more. Get rid of the boring, error prone task of wiring up MVC in Sitecore, improve performance and simplify your MVC strategy.
Installation
In Visual Studio, fire up the Package Manager console and install into your ASP.NET Web Application project(s)
PM> Install-Package Constellation.Foundation.Mvc
Please note that this package ships with a configuration file. If you load this NuGet package into a .NET Class Library project, you will have to ensure that the configuration file is deployed to your Sitecore installation, otherwise very few features will work and you will likely experience runtime errors.
If you are using Helix principles, and you’re using Areas for your Project-level Helix components, the entire package is now ready for use. If you are not using Areas, you will need to modify a configuration setting in order for the automatic View resolution to work.
Assumptions
This package works best if:
- Every Website in your Sitecore installation has an MVC Area named after the Project. (this is a guideline)
- You use Controller Renderings almost exclusively.
- You follow Sitecore’s lead with only one Action per Controller, named “Index.” (this is a guideline, you can deviate, however controller resolution may not work in these cases)
- You favor putting your presentation-level components at the Helix Project Layer.
Features
Automatic Controller Resolution
If you have a Rendering Definition Item, and it’s template is “Controller Rendering”, and the destination Controller’s name is of the format [Rendering Item Name]Controller, you may omit supplying the Controller parameter from the Rendering Definition Item.
Example:
You have a Controller named “CallToActionController” and you need to create a Rendering Definition to support it. Here’s all you need to do:
You can Save the Rendering Item and publish it. Constellation will handle the rest.
How it Works:
The supplied *.config file overrides the <mvc.getRenderer>
pipeline, replacing the stock “GetControllerRenderer
” processor with a slightly smarter one. If the Controller field is empty, the new pipeline processor will convert the human-legible name of the Rendering into a valid C# class name. This is then passed into the pipeline for resolution using stock MVC technology. Sitecore is already doing something similar for the Controller Action field, which will default to “Index” if no value is supplied.
Extended Rendering Cache Keys
Constellation allows you to have two Renderings on the same page with the same Datasource and be able to cache the HTML output of both. While this may seem obvious, Sitecore’s stock cache key generator does not include any facts about the Rendering Item itself, only the Datasource, thus only the first matching Rendering would be cached, and replayed over the second Rendering.
How it Works:
The supplied *.config file overrides the <mvc.renderRendering>
pipeline, replacing the stock “GenerateCacheKey
” processor with a slightly smarter one. When the cache key is generated, the “Short ID” of the Rendering Definition Item is added to the key during compositing, ensuring that your output HTML has a truly unique key.
Minify HTML, CSS, and Javascript in Rendering Output
Constellation uses the WebMarkupMin library to take a Rendering’s output and apply various whitespace, tag duplication, javascript minifcation, etc. to the output. Because WebMarkupMin is its own library with its own settings, you can adjust the degree of minification. Constellation offers a very DOM-safe default with “reasonable” compression, limited to the removal of white space, carriage returns and comments. The resulting compressed output is stored in Sitecore’s HTML output cache to save on CPU cycles the next time the Rendering is called.
How it Works:
The supplied *.config file overrides the <mvc.renderRendering>
pipeline, replacing the stock “AddRecordedHtmlToCache
” processor with a slightly smarter one. The rendering output is compressed using WebMarkupMin just before it is added to the output cache.
Automatic View Path Resolution
In most Sitecore projects, the file path to a given View matches the “end” of the Full Path to the Rendering Definition Item in Sitecore. We can take advantage of that fact to allow developers to “assume” the view’s location by convention rather than declare it. There are two components to this feature, although the second builds on the first.
IViewPathResolver
This is a standalone class that needs a Rendering Definition Item. You can inject this object into your controller as follows:
public ControllerName(IViewPathResolver viewPathResolver)
Calling IViewPathResolver.ResolveViewPath(RenderingItem) will produce a relative path you can pass to your Controller’s View(path, model) ActionResult.
The provided ViewPathResolver looks at the following settings provided in the *.config file:
Constellation.Foundation.Mvc.RenderingItemPathRoot
Specifies the Sitecore Content Tree path from the /sitecore node down to where the path is specific to Renderings.Constellation.Foundation.Mvc.ViewRootPath
Specifies the file system path from the root (~/) of the website down to where the path is specific to Views.
There is support for an “$Area” token, which will replace “$Area” with the value of the Rendering Definition Item’s “Area” field. If you’re not using Areas, this wildcard should be removed from both settings.
ConventionController
Designed as the primary interaction point for this library, ConventionController is a base-class controller that implements the best practices listed here.
- It is designed to be a single-Action controller, with the action named “Index”.
- It connects to ViewResolver and automatically resolves the View by name with no developer activity required.
- It supplies an abstract method for creating the Model to hand to the View, and provides developers with both the Rendering’s Datasource Item (if specified) and the Sitecore.Context.Item.
Usage
Create a new Controller and inherit from ConventionController. Implement the required Abstract methods. Create matching Rendering Definitions in Sitecore, ensure that the Controller, the Rendering Definition, and the View names (and locations) match.