Geeks With Blogs

News
Elton Stoneman (@EltonStoneman) IT Consultant, integration specialist, @Microsoft MVP and @Pluralsight author.

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:

Html.Sitecore().CurrentItem.Fields["Title"].Value


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:

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
       …
      <add namespace="Sitecore.Mvc" />
     </namespaces>
  </pages>
</system.web.webPages.razor>


- and in the config transform, Views\Web.NoSiteCore.config, replace the real assembly with your stub:

  
  <system.web.webPages.razor>     <pages pageBaseType="System.Web.Mvc.WebViewPage">       <namespaces xdt:Transform="Replace">       …         <add namespace=" SiteCoreStub"  />       </namespaces>     </pages>   </system.web.webPages.razor>

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: 

<ItemGroup> 
    <ProjectReference Include="..\SiteCoreStub\ SiteCoreStub.csproj" Condition="'$(Configuration )' == 'NoSiteCore'"> 
      <Project>{9043e623-9556-xyz-804b-cdb621872032}</Project> 
      <Name>SiteCoreStub</Name> 
    </ProjectReference> 
  </ItemGroup>


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.

Posted on Tuesday, July 16, 2013 3:20 PM Code Snippet , github , ASP.NET MVC | Back to top


Comments on this post: Developing a SiteCore MVC site without SiteCore

# re: Developing a SiteCore MVC site without SiteCore
Requesting Gravatar...
This is a real relief to have found your straightforward instructions for building a SiteCore MVC site without SiteCore that can be helpful for this very important project and hopefully others as well.
Left by Jack Miller on Feb 20, 2014 8:58 PM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net | Join free