Starting with MVC development

This page provides instructions on how to set up an MVC application together with a Kentico instance so that you have a starting point for further MVC development.

Kentico provides ASP.NET MVC support based on a separate MVC application. The application takes care of the presentation and is separated from the Kentico administration. Both applications access the same database as the Kentico application and content synchronization can be handled by web farms.

Licensing for MVC applications

You only need to purchase a license for the MVC application. The additional license for the Kentico administration and one extra web farm server is provided for free. To obtain the additional license, contact our sales department.

Setting up the Kentico instance

In order to develop an MVC application with Kentico, you need to use a content only site that will serve as a repository for content:

  1. Go to the Sites application.
  2. Click on the New site wizard button.
  3. Select Use web template.
  4. Click Next.
  5. Select the MVC Blank Site template from the list.

    MVC Blank Site template selection

  6. Continue with the remaining steps of the wizard and finish creating the new site.

Now edit () your new site and on the General tab set the Presentation URL. This is the URL that will be used by the MVC application. For example, http://localhost/MvcApplication or http://www.SiteDomain.com.

Next, you need to set up a content sychronization mechanism:

  1. Go to the Settings application.
  2. Navigate to Versioning & Synchronization -> Web farm.
  3. Set the Web farm mode to Automatic.
  4. Click the Save button.

Both applications on the same domain

If you plan to run the Kentico and MVC applications on the same domain (for example, distinguished by port numbers and run in different virtual folders), you need to manually assign unique server names to the web farm servers. To set a web farm server name, see Step 5 in Configuring web farms manually.

At this point the content only site and your Kentico instance are set up to be used with an MVC application.

Setting up the MVC application

Your new MVC application needs to be configured to work with Kentico:

  1. Create an empty ASP.NET MVC project in Visual Studio.
    • Select the Empty template.
    • Enable the MVC checkbox to add folders and core references for MVC.

      Creating a new MVC application in Visual Studio

  2. Right-click the solution and select Manage NuGet Packages for Solution.
  3. Install the Kentico.Web.Mvc integration package in your MVC application project.
  4. Edit the MVC project's main web.config file:

    • Add a connection string to your Kentico database. We recommend copying the exact connection string from the web.config file of the related Kentico application project.

      <connectionStrings> 
          <add name="CMSConnectionString" connectionString="Persist Security Info=False;database=Kentico;server=myserver;user id=username;password=mypassword;Current Language=English;Connection Timeout=120;" />
      </connectionStrings>
    • Copy the value of the CMSHashStringSalt appSettings key from the web.config of the Kentico project and add the value into the same key in the MVC project's web.config (it is empty by default). This ensures that the MVC application generates hashes using the same salt value as the Kentico application (for example for macro signatures or page preview links).

      <appSettings>
      	<add key="CMSHashStringSalt" value="e68b9ad6-a461-4707-8e3e-ece73f03dd02" />
      	...
      </appSettings>
  5. In the MVC application's Global.asax file, add a reference to Kentico.Web.Mvc and add the following line in the Application_Start method:

    using Kentico.Web.Mvc;
    ...
    
    protected void Application_Start()
    {
    	...
    
    	// Enables and configures the selected Kentico ASP.NET MVC integration features
    	ApplicationConfig.RegisterFeatures(ApplicationBuilder.Current);
    }
  6. In the App_Start\RouteConfig.cs file, add a reference to Kentico.Web.Mvc and call the MapRoutes method to register Kentico HTTP handlers before mapping any of your routes.

    using Kentico.Web.Mvc;
    
    public static void RegisterRoutes(RouteCollection routes)
    {
    	routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
    	// Maps routes to Kentico HTTP handlers.
    	// Must be first, since some Kentico URLs may be matched by the default ASP.NET MVC routes,
    	// which can result in pages being displayed without images.
    	routes.Kentico().MapRoutes();
    
    	...
    } 
  7. In the Properties\AssemblyInfo.cs file, add the AssemblyDiscoverable assembly attribute. The change allows you to work with the classes generated for Kentico objects.

    using CMS;
    ...
    [assembly: AssemblyDiscoverable]
  8. Set a URL for the MVC application:
    • Right-click the MVC project and select Properties.
    • Click on the Web category.
    • In the Servers section, select Local IIS.
    • Set the Project Url identical to the Presentation URL assigned in your MVC site's settings in Kentico.
    • Click Create Virtual Directory.

If run, the MVC application will be visible in the Kentico's Web farm application on the Servers tab as a new server and its Status changes to Healthy after a few moments.

You can now continue to define the content structure of your site and develop the MVC application.


Was this page helpful?