---
title: Adding preview mode support for MVC sites
related:
  - https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc.md
  - https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md
  - https://docs.kentico.com/k10/developing-websites/defining-website-content-structure/page-types/configuring-page-types/specifying-the-url-pattern-for-content-only-pages.md
  - https://docs.kentico.com/k10/managing-website-content/working-with-pages/sending-links-to-unpublished-pages.md
  - https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/improving-performance-of-mvc-applications.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

Preview mode in Kentico provides a way to view the latest version of pages before they are published (for example when using [Workflow](https://docs.kentico.com/k10/managing-website-content/working-with-pages/using-workflows.md)). If you wish to use preview mode in an MVC application that handles the presentation of the website's pages, you need to implement the required functionality in the application's code.

Before you can use preview URLs, you need to [specify the URL pattern](https://docs.kentico.com/k10/developing-websites/defining-website-content-structure/page-types/configuring-page-types/specifying-the-url-pattern-for-content-only-pages.md) (in Kentico administration) for your content only page types. The URL pattern is then used to create URLs to the Kentico content presented by an external application. Preview URL for pages are generated:

- When using the main **Preview** mode in the **Pages** application.
- When generating preview links for pages in **Pages -> Properties -> General -> Preview URL**. See: [Sending links to unpublished pages](https://docs.kentico.com/k10/managing-website-content/working-with-pages/sending-links-to-unpublished-pages.md)

The preview URLs the system generates for content only pages consist of additional information, such as language version and a hash for validating the URL. The Kentico [integration package](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) in your MVC application validates and processes the preview URL. The package then removes the additional information to leave only the original presentation URL based on the URL pattern. The original presentation URL then goes through the standard routing process.

> **Note:** **Note**: You need to manually ensure that your MVC application displays the **latest versions of pages** in preview mode. See [Loading the latest versions of pages](#loading-the-latest-versions-of-pages) for details.

## Enabling preview mode in MVC applications

Preview mode support is provided in the [Kentico.Content.Web.Mvc integration package](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md). To enable preview support in your MVC application:

1. If necessary, set up your Kentico instance and MVC application (see [Starting with MVC development](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development.md)).
2. [Specify a URL pattern](https://docs.kentico.com/k10/developing-websites/defining-website-content-structure/page-types/configuring-page-types/specifying-the-url-pattern-for-content-only-pages.md) for content only pages.
3. [Install](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) the **Kentico.Content.Web.Mvc** integration package in your MVC project.
4. Enable the preview feature in the MVC application's **ApplicationConfig.cs** file:

   ```csharp

   using Kentico.Content.Web.Mvc;

   ...

   public static void RegisterFeatures(ApplicationBuilder builder)
   {
       ...
       builder.UsePreview();
       ...
   }

   ```

   > **Note:** **Framing policy and preview mode**
   >
   > By default, the MVC application sends the [X-Frame-Options HTTP header](https://en.wikipedia.org/wiki/Clickjacking#X-Frame-Options) with the _SAMEORIGIN_ framing policy. The [Kentico integration package](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/starting-with-mvc-development/installing-kentico-integration-packages.md) disables the header for preview URLs. This allows users to preview content handled by the MVC application in the Kentico administration interface, even when running the administration on a different domain.

### Excluding tracking scripts for preview mode

If you use third-party analytics or tracking scripts on your website (for example Google Tag Manager scripts), we strongly recommend that you disable them for preview mode. The preview URLs are intended for internal use within the Kentico administration interface or for viewing unpublished pages. Exposing preview URLs to third parties and having them affect your website's analytics data is typically not desired.

To exclude tracking scripts for preview mode, use conditions that [evaluate whether preview mode is enabled for requests](#checking-whether-preview-mode-is-enabled-for-requests). You can add the conditions directly around the script code within your site's views, or within related business code that you use to include the scripts.

### Checking whether preview mode is enabled for requests

Use the following code to check whether preview mode is enabled for a request:

```csharp

using Kentico.Content.Web.Mvc;

...

bool previewEnabled = System.Web.HttpContext.Current.Kentico().Preview().Enabled;

```

The location where you need to check the request context for preview mode depends on the implementation of your MVC application. For example, directly in the code of your controllers or when registering repositories that make use of your [generated providers.](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md)

> **Tip:** **Tip**: If the _URL pattern_ that you use for your content only pages does not include a variable for the page culture (for example if you use a different mechanism to pass the requested culture), you can get the culture from the context of the preview request:
>
> ```csharp
>
> using Kentico.Content.Web.Mvc;
>
> ...
>
> string cultureName = System.Web.HttpContext.Current.Kentico().Preview().CultureName;
>
> ```

### Loading the latest versions of pages

For requests that have preview mode enabled, you need to ensure that your code loads the **latest** versions of pages instead of the _published_ versions. For more information about retrieving page data, see [Generating classes for Kentico objects](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/generating-classes-for-kentico-objects.md). For example:

```csharp

// Uses DocumentQuery to load article pages, getting the latest versions instead of the published versions
var articles = ArticleProvider.GetArticles()
                                .LatestVersion(true)
                                .Published(false)
                                .OnSite("mvcsite")
                                .Culture("en-us")
                                .OrderByDescending("DocumentPublishFrom");

```

If you use [data caching](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc/developing-mvc-applications/improving-performance-of-mvc-applications.md) for your pages, you need to disable it for preview mode. This ensures that caching works correctly for standard requests, but is not used for the different content displayed in preview mode.

> **Info:** **Preview mode and controller output caching**
>
> If you use caching of controller output in your MVC application ([OutputCache](https://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute%28v=vs.100%29.aspx) attributes), the [Kentico integration package](https://docs.kentico.com/k10/developing-websites/developing-sites-using-asp-net-mvc.md) in your MVC application automatically ensures that the output cache is not used for page requests in preview mode.

Preview mode should now work correctly for the Kentico application that you use to manage your website's content.

> **Info:** See our [MVC Demo site](https://github.com/Kentico/Mvc) for an advanced example of an MVC application that uses preview mode.
