We’ve just kicked off a new project delivering a partner website using SiteCore, the commercial ASP.NET Content Management System. We’re running the latest version, so outside of the CMS templates, the transactional code is all done in ASP.NET MVC, hitting services in a hybrid cloud/on-premise setup.
We’re running a mixed team, so not all front-end developers will be SiteCore developers, and I don’t really want the MVC guys (myself included) having the distraction of installing and running the site under CMS while they’re developing.
SiteCore integrates with ASP.NET MVC views using extension methods to MVC’s HtmlHelper class, so to render a CMS item in a view you do this:
Extension methods are only resolved by namespace, so it’s very easy to swap out your own stub implementation of the Sitecore class – it only needs properties and methods which match the signature of anything you use from the real Sitecore class, you don’t need to implement an interface or stub out the whole class.
So here’s how to develop the MVC part of SiteCore projects without SiteCore installed.
Firstly make sure you only reference the namespace of your extension methods in Web.config in the Views folder, not with @using statements in each view.
Next you can add a custom solution configuration which will build the project in “no SiteCore” mode, with a config transform to use your stub Sitecore implementation.
In your Views\Web.config add the real SiteCore namespace:
<add namespace="Sitecore.Mvc" />
- and in the config transform, Views\Web.NoSiteCore.config, replace the real assembly with your stub:
<add namespace=" SiteCoreStub" />
Your SiteCore MVC project will need to have a reference to the stub assembly, but you can condition that in the .proj file so it only gets built in the NoSiteCore configuration. Easiest way is to add a project reference as normal then unload the project and edit the proj file, adding the configuration condition:
<ProjectReference Include="..\SiteCoreStub\ SiteCoreStub.csproj" Condition="'$(Configuration )' == 'NoSiteCore'">
If you look at the Web.config for the project, there’s 3,500 lines of SiteCore configuration, directing requests to the SiteCore runtime and the SiteCore membership providers etc., which we don’t want, so in the site’s Web.NoSiteCore.config transform file, you’ll want to pull all that out.
You can use the HtmlHemlperExtensions and Web.NoSiteCore.config transforms from this gist to get you started: SiteCoreStub-HtmlHelperExtensions.cs.
Then all you need to do is add a publish profile to the project, publishing the NoSiteCore configuration to the local filesystem. Set that location as a directory in IIS, browse to it and you get your MVC output with markers for the SiteCore content, and the MVC guys can develop away without needing SiteCore.
That also gives you a nice exit path if you’re evaluating SiteCore for an MVC solution, and want the option to remove the CMS part – you know your MVC site works without SiteCore so if you decide to go plain MVC, it’s just a case of replacing the Sitecore content with MVC.