Is this really the version you are looking for? Kentico Xperience 11 Tutorial is no longer updated. Use the switcher in the header to choose your product version.
This page is a part of a tutorial, which you should follow sequentially, from the beginning to the end. Go to the first page: Getting started with Kentico.
In the previous step of the tutorial, you have set up the Kentico instance that will serve as your MVC site's content repository. In this step, you will create the MVC project that will present the live site and configure it to work with your Kentico instance.
Enter MedioMVC as the project's name.
Select a location for your project.
To prevent conflicts in permissions, we recommend that you save your MedioMVC tutorial project in the same folder where you have installed Kentico 11, for example: C:\inetpub\wwwroot\
To help you start developing the MVC application, Kentico provides integration (NuGet (opens in a new tab)) packages you can install through Visual Studio. For the functionality covered in this tutorial, you need to install the Kentico.Web.Mvc and Kentico.Content.Web.Mvc packages.
The Kentico.Web.Mvc package contains the necessary items which will make your MVC application work with the Kentico application. The additional Kentico.Content.Web.Mvc NuGet package contains API that will enable previewing of pages in the administration interface.
To ensure that these items work in your MedioMVC solution, you need to configure several key elements in your MVC project.
In the App_Start folder, edit the ApplicationConfig.cs file. The class was added to your project automatically by the Kentico.Web.Mvc integration package.
Add a using statement for the Kentico.Content.Web.Mvc namespace and call builder.UsePreview() in the RegisterFeatures method.
using Kentico.Web.Mvc;
using Kentico.Content.Web.Mvc;
namespace MedioMVC
{
public class ApplicationConfig
{
public static void RegisterFeatures(ApplicationBuilder builder)
{
builder.UseDataAnnotationsLocalization();
builder.UseNotFoundHandler();
// Enables the Preview mode functionality, which allows your website editors to preview
// the content of the MVC site's pages from the Kentico user interface.
builder.UsePreview();
}
}
}By adding the ApplicationConfig.RegisterFeatures(ApplicationBuilder.Current) method to the Global.asax file, you have made sure that your configuration in ApplicationConfig.cs will be executed on the application start. Your MedioMVC project is now ready for integration with the Kentico instance. To find out about more about the integration packages you have installed, see Installing Kentico integration packages.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
<!--
The MVC application will use the database of your Kentico instance
Change the values of database, server, user id and password to your match your connection string
-->
<connectionStrings>
<add name="CMSConnectionString" connectionString="Persist Security Info=False;database=Kentico11;server=myserver;user id=username;password=mypassword;Current Language=English;Connection Timeout=120;" />
</connectionStrings>
...
</configuration>Some Kentico features utilize hashing (macros, preview URLs, etc.). To ensure correct behavior of the system, both the MVC and Kentico applications need to generate hashes using the same salt value.
Navigate to the Kentico web.config file.
Locate CMSHashStringSalt among the <appSettings> keys.
Copy the value of the CMSHashStringSalt key into the same key in the MedioMVC project's web.config (the hash salt value is empty in the default web.config).
<!-- Example of app settings in the MedioMVC project with the "CMSHashStringSalt" key -->
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<!-- CMSHashStringSalt key -->
<add key="CMSHashStringSalt" value="0bc1b86a-3fc8-4305-a8c3-20f5141aaf0a" />
</appSettings>Your applications now use the same hash salt value. For example, in this tutorial, the identical hash salts allow page preview URLs to work correctly. The hash salt configuration also enables other features not covered by the tutorial, for example resolving of macros in emails based on templates, which are defined in the Kentico administration interface, but actually sent by the MVC application in response to live site user actions.
The Kentico API uses HTTP handlers with specific URLs to retrieve content and perform various other functions (for example /getmedia and /getattachment handlers that display images and other file content). To make sure your MVC application directs such URLs to the appropriate handlers, you need to register Kentico specific routes in your MVC application's route table.
Kentico routes must be mapped before you map other more general routes for your site's content. Otherwise the URLs of Kentico HTTP handlers could match your custom routes, which would result in missing content or incorrect behavior.
In Visual Studio, edit the RouteConfig.cs file in the App_Start folder.
Add a using statement for the Kentico.Web.Mvc namespace.
Call the Kentico().MapRoutes method above the DefaultMapRoute method.
using System.Web.Mvc;
using System.Web.Routing;
using Kentico.Web.Mvc;
namespace MedioMVC
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Maps routes to Kentico HTTP handlers
routes.Kentico().MapRoutes();
...By mapping the Kentico routes, you have made sure that Kentico HTTP handlers will work correctly on your MVC site. Remember that you have to call the Kentico().MapRoutes method before you map other more general routes for your site's content.
You will often need to extend your MVC project by adding and registering classes. For example, later in the tutorial you will generate classes representing page types and add them to your MVC project.
Kentico application code uses a discovery process to detect various types of registered classes during initialization. For performance reasons, only code assemblies that are marked as discoverable are processed. As a result, you need to make sure the assembly compiled from your MVC project is discoverable.
using CMS;
// Ensures that the Kentico API can work correctly with generated and custom classes registered in the MVC project
[assembly: AssemblyDiscoverable]By adding the AssemblyDiscoverable attribute, you ensure that the Kentico discovery process includes your MVC project's code assembly. This allows the application to work with various types of custom components added by developers (such as generated classes for page types).
The attribute is placed into a separate class because the default AssemblyInfo class cannot access code from external libraries in cases where the web project is precompiled with outputs merged into a single assembly (during deployment).
You want your MVC application to run under a specific URL, which you defined while setting the Kentico site. You need to register an IIS application for your MVC project with the given URL.
Running your MVC project on the same URL as the Presentation URL set for your Kentico content only site allows content editors to preview pages from the Kentico interface, add hyperlinks to pages in text editor fields, and more.
You are all set! You have created the MVC application and adjusted all the necessary configuration. Your MVC application is now connected to your Kentico site, and you can start building your site's pages in the next step.
Previous page: Creating a new MVC site — Next page: Creating content only page types
Completed pages: 3 of 10
ΟΟΟΟΟΟΟΟΟΟ
Save your changes.
Edit your project's Global.asax file.
Add a using statement for the Kentico.Web.Mvc namespace.
In the Application_Start method, call the ApplicationConfig.RegisterFeatures(ApplicationBuilder.Current) method.
using System.Web.Mvc;
using System.Web.Routing;
using Kentico.Web.Mvc;
namespace MedioMVC
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
// Enables and configures the selected Kentico ASP.NET MVC integration features for the MedioMVC application
ApplicationConfig.RegisterFeatures(ApplicationBuilder.Current);
}
}
}Save your changes.
Save your changes.
Save your changes