Sitecore Blog: @sitecorejohn blog

Sitecore MVC Playground Part 1: Enable MVC

By John West, June 20, 2012 | Rating:  | Comments (27)

I intend this to be the first in a series of blog posts that will help developers start working with and learning the MVC features in version 6.6 and later of the Sitecore ASP.NET web Content Management System (CMS). I based the information in this blog post on an internal build of Sitecore CMS 6.6, which I think should be very close to the beta available towards the end of June and the eventual version release. This blog post repeats some of the information available in the blog post Using Web Forms and MVC in a Single Solution with the Sitecore ASP.NET CMS and the comments on that post. For more information about using MVC with Sitecore, see the blog post Posts about Using MVC with the Sitecore ASP.NET CMS.

Install Sitecore by following the instructions in The Sitecore Installation Guide. I used the setup program and named my instance playground. One thing to beware: on the IIS Web Site page of the installer, click Advanced, and then select .NET Framework Version 4.0. You can use IIS Manager to update the framework version associated with the application pool. If you use the wrong version, you will likely receive the error "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded".

After you install Sitecore, follow these steps to configure the new instance to support MVC:

  1. Move the files from the /bin_Net4 subdirectory to the /bin subdirectory, and then delete the empty /bin_Net4 subdirectory.
  2. Rename the /Web.config file to /Web.config.webforms.
  3. Rename the /Web.config.MVC file to /Web.config.
  4. Copy the value of the attribute named value of the /configuration/sitecore/sc.variable element named dataFolder in the /Web.config.webforms file to overwrite the corresponding attribute in the /Web.config file.
  5. Rename the /App_Config/Include/Sitecore.Mvc.config.disabled to Sitecore.Mvc.config. For information about Web.config include files, see the blog post All About web config Include Files with the Sitecore ASP.NET CMS.
  6. Rename the /App_Config/Include/Sitecore.Mvc.ExperienceEditor.config.disabled file to Sitecore.Mvc.ExperienceEditor.config.
  7. Rename the /App_Config/Include/Sitecore.Mvc.Simulator.config.disabled file to Sitecore.Mvc.Simulator.config.
  8. Optionally, if you plan to use the Digital Marketing System (DMS), rename the /App_Config/Include/Sitecore.Mvc.Analytics.config.disabled Web.config include file to Sitecore.Mvc.Analytics.config. I haven’t found any harm in doing this on systems on which I have not installed DMS.
  9. Optionally, you can delete /default.aspx or rename it to something like /default.aspx.bak (just in case anyone tries to access the site using the equivalent of http://playground/default.aspx, which would otherwise bring up the default Web Forms home page rather than the MVC home page).
  10. Install MVC 3. If you neglect this step, you will likely receive an error message such as "Could not load file or assembly System.Web.Helpers…or one of its dependencies…The system cannot find the file specified". There are other ways to install the required assemblies if you don’t plan to create an MVC project in Visual Studio, but this is the easiest, and you will almost certainly want to create such a project in the future.

To confirm MVC is working:

  1. Create a the subdirectory /Views containing a nested subdirectory named Sitecore.
  2. Create the /Views/Sitecore/index.cshtml file containing some Razor code (note that the @inherits directive is not necessary if the /Views subdirectory contains the proper web.config file, such as that installed when you create an MVC project in Visual Studio):
    @inherits System.Web.Mvc.WebViewPage 
    @{
      Layout = null;
    }
    <html>
      <body>
      <h4>Index.cshtml</h4>
      <div>Model:
    @{
      if (Model == null)
      {
    <text>null</text>
      }
      else
      {
    @Model.GetType();
      }
    }
        </div>
      </body>
    </html>
  3. In the Sitecore Content Editor user interface, navigate to the /sitecore/layout/Layouts/Sample Layout item that defines the default layout.
  4. Click the Content Tab.
  5. In the Path field, enter /Views/Sitecore/index.cshtml, and save.
  6. Either publish the layout definition item and view the home page of the published site (http://playground – not http://playground/default.aspx), or use the Page Editor. You should see content such as the following:

Index.cshtml
Model: Sitecore.Mvc.Presentation.RenderingModel

I think that’s all it takes. If anyone follows this process, please comment about whether it worked for you.

Next in this series: Sitecore MVC Playground Part 2: Working without a (Visual Studio Dot) Net

Tags: Architecture, Infrastructure, MVC

Comments

  • What is internal build and how can we get it?

    - Daryl Macam
    July 08, 2012 at 4:48 PM

  • If you cannot access this:

    http://sdn.sitecore.net/SDN5/Products/Sitecore%20V5/Sitecore%20CMS%206/Update/6_6_0_rev_120622.aspx

    Then I think you need to http://sitecore.net/contact Sitecore in your region and ask about your SDN permissions.

    - John West
    July 09, 2012 at 8:49 AM

  • The above steps worked for me. Thanks John!

    - Mike Lopez
    August 09, 2012 at 11:55 AM

  • Worked like a charm! :)

    - Joseph Baarsch
    August 20, 2012 at 11:19 AM

  • Hi John, I have just tested it on the cms 6.6 build 120981. It works fine. We need to change the steps to accommodate changes brought by the current release.

    - Sen Gupta
    November 08, 2012 at 6:06 AM

  • @Sen:

    I have to go through all of my old posts about MVC to see what has changed in the release, so I will investigate this as well. Do you have any specifics that you could post now and for me to consider later? When I ran the installer without MVC support, it didn't even install the files I would need for MVC, so you must have used the .zip (and from the sound of it, that defaults to no MVC support?) I already posted about the release installer enabling MVC by default, which obviates the need for these manual steps:

    http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2012/11/Sitecore-ASPNET-CMS-6-6-Installer-Enables-MVC-by-Default.aspx

    Thanks & regards,

    -John

    - John West
    November 08, 2012 at 9:18 AM

  • Worked like a charm
    Thanks!

    - Jose Marcenaro
    December 11, 2012 at 8:04 PM

  • John,

    I did a presentation on MVC, has the document and PPT listing the steps.
    Please let me know, if we need to share it. :-)

    - Sen Gupta
    December 12, 2012 at 7:27 AM

  • @Sen: I would like to see that PPT! Can you email it to me or share it on an FTP server or something?

    - John West
    December 12, 2012 at 11:55 AM

  • Worked great. Rgs

    - Anthony Hook
    December 13, 2012 at 11:08 AM

  • I've downloaded the latest drop - Sitecore.NET 6.6.0 (rev. 121203).

    It would appear that this process has changed with the latest release. Any insight as to how to configure for MVC now? I suspect it is even easier!

    Thanks,

    - Brian Ryther
    December 18, 2012 at 12:46 PM

  • Brian - the easiest way is definitely the installer, which enables MVC support by default. I don't know of anything that automatically creates Visual Studio projects for Web Forms, MVC, or both together in a single solution, so I would still create those manually.

    - John West
    December 18, 2012 at 1:21 PM

  • This worked perfectly with 6.6.0, rev. 121203. Thanks for writing this up.

    Please note that the documentation at http://sdn.sitecore.net/Reference/Sitecore%206/MVC%20Reference.aspx does not mention all of the config.disable files that need to be renamed, and should be updated.

    - Dan Solovay
    December 27, 2012 at 12:19 PM

  • Also worked for me with 6.6.0, rev. 121203.

    - Wesley da Silva
    January 07, 2013 at 5:31 AM

  • Hi John, thanks for this. The only thing is I found I typo (maybe just changed on Sitecore.NET 6.6.0 rev. 130111) , but you should use @Html.Sitecore().Placeholder("main") instead of "PlaceHolder" with capital h I was getting the error message "Sitecore.Mvc.Helpers.SitecoreHelper' does not contain a definition for 'PlaceHolder' . Cheers.

    - Jorge Lusar
    February 19, 2013 at 3:07 AM

  • how about Sitecore 6.6.0 rev. 130111? it seems some steps has already been done.

    - Borj Ano
    February 20, 2013 at 4:08 AM

  • in Sitecore 6.6.0 rev. 130111, I dont have to do 1-10, I do the confirm steps and it works!

    - Borj Ano
    February 20, 2013 at 6:42 AM

  • @Borj: Did you install using the installer (exe), or the zip? I know that newer versions of the installer enable MVC by default, but I haven't checked the zip distributives.

    - John West
    February 20, 2013 at 8:50 AM

  • Is it possible to enable razor as the default view engine so that when a (sub)layout is created, it creates a cshtml file rather than an aspx file?

    - Richard Richard
    February 25, 2013 at 6:40 PM

  • I think so, but I would avoid it. I would instead create View Renderings. If that approach does not set the file path field correctly, or create the file, or if you're talking about Rocks, then I would work on enhancing the process for creating view renderings (or getting Sitecore to do that). You might want to removed the Sublayout template from insert options so nobody uses it by accident.

    - John West
    February 26, 2013 at 6:54 AM

  • Sitecore 6.6.0 rev. 130214 - zipped webroot with Visual Studio 2012.

    Works great :)

    I did not do step 10, since VS 2012 already have MVC 3 and MVC 4 out of the box.

    - Thai Nguyen
    March 12, 2013 at 3:36 AM

  • do we have step by step tutorials on sitecore MVC setup, because when i installed Sitcoer 6.6 i don't have the
    bin_Net4, Web.config.MVC.
    Or am i doing wrong some thing, please help.

    - Deepak Gedia
    April 10, 2013 at 11:14 PM

  • Hi Deepak,

    In Sitecore 6.6 MVC is configured by default so you don't need to make any changes unless you would need some special configuration.

    While, you install Sitecore 6.6 observe the advance options on all the wizard dialogs, they reveal a lot. ;-)

    You know where to ping me in case you need any help.

    - Sen Gupta
    April 11, 2013 at 7:59 AM

  • The Sitecore 6.6 installer enables MVC by default. Do you have a /App_Config/Include/Sitecore.mvc.config file in your installation? Did you install from ZIP?

    - John West
    April 11, 2013 at 10:33 AM

  • Sitecore 7 web.config include file names may be slightly different:

    Sitecore.Mvc.config
    Sitecore.MvcExperienceEditor.config
    Sitecore.MvcSimulator.config
    Sitecore.MvcAnalytics.config

    - John West
    June 04, 2013 at 8:38 AM

  • Hi John,

    Thanks for sharing info it works to me as well.

    waiting for more tutorials about sitecore mvc

    - salman ansari
    August 12, 2014 at 3:42 AM

  • Hi John,

    Does Sitecore 6.6.0 supports MVC 4 ?

    Thanks

    - Marina Rakhmilevitch
    October 01, 2014 at 4:20 PM

*{0} must be filled in.
*{0} must be filled in.
*{0} must be filled in.