Skip to content

Setup ASP.NET MVC 4 project

Jeremy Cook edited this page May 9, 2016 · 12 revisions

Prerequisites:

  • ASP.NET MVC 4 website project with .Net 4.0

Note: this tutorial is available in video format: http://www.youtube.com/watch?v=mHJzmsFPrM4

Package installation

In NuGet console, paste the line below:

Install-Package BetterCMS

This will install the Better CMS NuGet package and all the dependent packages.

Update Global.asax.cs

After a successful Better CMS package installation, update your Global.asax.cs file with the following usings:

using System.Security.Principal;
using BetterCms.Core;
using BetterCms.Core.Environment.Host;

and code:

private static ICmsHost cmsHost;

protected void Application_Start()
{
    cmsHost = CmsContext.RegisterHost();

    /* DO NOT FORGET TO REMOVE DEFAULT ROUTE REGISTRATION! 
       FOLLOWING SOURCE CODE SHOULD BE REMOVED: 

       routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
    */

    // [YOUR CODE]	
		
    cmsHost.OnApplicationStart(this);
}

protected void Application_BeginRequest()
{
    // [YOUR CODE]
	
    cmsHost.OnBeginRequest(this);
}

protected void Application_EndRequest()
{
    // [YOUR CODE]
	
    cmsHost.OnEndRequest(this);
}

protected void Application_Error()
{
    // [YOUR CODE]
	
    cmsHost.OnApplicationError(this);
}

protected void Application_End()
{
    // [YOUR CODE]
	
    cmsHost.OnApplicationEnd(this);
}

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    // [YOUR CODE]

    // Uncomment following source code for a quick Better CMS test if you don't have implemented users authentication. 
    // Do not use this code for production!
    /*
    var roles = new[] { "BcmsEditContent", "BcmsPublishContent", "BcmsDeleteContent", "BcmsAdministration" };
    var principal = new GenericPrincipal(new GenericIdentity("TestUser"), roles);
    HttpContext.Current.User = principal;
    */

    cmsHost.OnAuthenticateRequest(this);
}

Register Routes

Better CMS adds default pages with / path - be sure to update your site routes configuration to not takeover the site root path. Do not forget to remove the default route registration, too.

Setup Database

You only need to create a database instance and update the connection string in Web.config to point to it (use named instance called BetterCms). Update Config/cms.config tag <database [...]/> with the correct information. All the necessary database structure (tables and etc.) will be created when the application starts. The default (if not set) database type is MsSql2008.

  <database
     schemaName="dbo"
     connectionStringName="DefaultConnection"
     databaseType="MsSql2008" >
  </database>

Other available DB types: MsSql2000, MsSql2005, Oracle10, Oracle9, PostgreSQL82. Currently only MS Sql Server and Azure is supported.

Note: If you have changed the database and would like for Better CMS to create all the structures in the new one, please delete the App_Data/BetterCMS/versions.info.cache file (this will force CMS to check the versions information in the database).

Web.config

Update Web.config file:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    [...]
  </assemblyBinding>
</runtime>

Check webpages version (webpages:Version) and disable simple membership provider (enableSimpleMembership) if you're not going to use it.

<appSettings>
  <add key="webpages:Version" value="2.0.0.0" />
  <add key="enableSimpleMembership" value="false" />
  [...]
<appSettings>

Add a current line to web.config's system.webServer section (Note: this is a temporary solution). This is required for such files as main.js to be loaded correctly:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

After these steps, Better CMS is ready to use.

Note: Do not forget about cache.

Clone this wiki locally