How To Architect Your Sitecore Website With MVC
There are many ways to set-up your Sitecore website. My personal preference is to use TDS and Glassmapper, however, on some projects the budget might not quite fit. In today's tutorial, I'm going to cover one solution for setting up your Visual Studio solution, how to write your code and how to debug it.
Installing SitecoreIn order to start building MVC websites using Sitecore, the first step is to install Sitecore. If you are completely new, then I recommend reading this, How To Install a Sitecore Instance Using the Sitecore Instance Manager. In this scenario, we're not going to build anything inside the Sitecore Website folder. All the code, CSS, HTML will be written in a separate folder and effectively copied in, using the visual studio publish method. Using this method means we upgrade Sitecore easily, delete the website to rebuild it easily, and make our source control repo a little easier to manage. Now, this approach does have some downfalls. Reading this, you might be wondering why this adds a bit more complexity to the solution. When you start writing code you need to do a publish each time. Also, you might be wondering how do you debug? It's pretty simple... attach the debugger to IIS and your breakpoints will fire (more on this later).
Creating Your MVC SolutionAs we are not building inside the Sitecore folder, we need to create a project that contains a 'Controller', 'Models' and 'Views' folder. To do this we simply create an empty MVC project and then delete the bits we don't need. So, in Visual Studio create a new ASP.NET Web Application project. Select an MVC project, you don't want to include anything else. You also want to create the site without all the OWIN crap, so don't forget to click 'Change Authentication' Now we have a solution, we want to delete all the things that we don't want to publish into the 'Website' folder that contains our Sitecore instance. In this example, I'm removing everything except the packages.config, Controllers, Views and Models. In your scenario, you might want to keep the web.config and global.ascx files to write custom logic in. To keep this tutorial simple, I'm removing them... but it's up to you. Now we have the skeleton outline for our project, we still have a few tweaks to do. In your 'Views' folder, you should have a web.config. In here you need to make sure you have the Sitecore MVC namespace added.
<namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Optimization"/> <add namespace="System.Web.Routing" /> <add namespace="Sitecore.Mvc" /> <add namespace="JonDJones.SiteCore.Mvc" /> </namespaces>I usually add my web projects default namespace as well. Next thing is to add references to the Sitecore assemblies. In your Sitecore instances bin folder, copy the Sitecore.MVC and Sitecore.Kernel assemblies. Create a new folder called 'Lib' in the same directory as your Sitecore instances websites and data folder (this is probably the same level you want to have your MVC project as well). Copy the Sitecore assemblies into the 'Lib' folder and then reference them in your web project through Visual Studio -> Add References. At this point, I would recommend checking your code into your source control repo, or at a minimum creating a file copy of the whole folder.