Displaying page content

To retrieve content from individual content-only pages in your MVC application, use the generated strongly typed classes provided by the system for each page type. You can use the retrieved page content to populate prepared view models and then display them in your views.

Generated provider classes facilitate the retrieval of individual pages of the corresponding page type. The following example demonstrates the use of a provider class generated for a sample Article page type:

// Gets the specified articles using the generated provider
IEnumerable<Article> articles = ArticleProvider.GetArticles()
    .OnSite("MySite")
	.Culture("en-US")
	.Path("/Articles/", PathTypeEnum.Children);

// Gets the published version of a single article using the generated provider
Article article = ArticleProvider.GetArticle(nodeGuid, "en-US", "MySite");

By default, the generated code returns published versions of pages. If you need to retrieve the latest edited version of a page, use the following approach:

// Gets the latest version of a single article using the generated provider
Article article = ArticleProvider.GetArticle(nodeGuid, "en-US", "MySite")
	.LatestVersion()
	.Published(false);

To filter the retrieved pages, use any of the query parameters for single page types type listed in Working with pages in the API.

Tip: Optimize the performance of your website by caching the retrieved page data.

Automatic URL resolving

The system provides page output filtering functionality that automatically resolves all virtual relative URLs to their absolute form. The URLs are processed on the side of the MVC application, based on the environment where the site is actually running.

This filter ensures that links added by content editors into page content work correctly, even if you do not explicitly handle URL resolving in your code.

Working with retrieved page data

You can access the data of a retrieved page using its properties. For general data, you can call the TreeNode.TryGetValue method and specify the name of the column you are trying to access. The types of page data you can access are:

  • Page field data – the data that is editable on a page's Content tab
  • Metadata – data like the page title and page keywords
  • Page attachments – stored in fields with the File or Attachments data type

For information about retrieving page attachments, see Displaying page attachments.

Accessing page field data

The fields available on a page's Content tab are specific to its Page type. You can see the fields that each page type uses:

  • In Page types (application) -> edit page type -> Fields tab
  • In the CONTENT_<document_type> database table

We recommend accessing specific fields of a page type via the Fields property:

// Retrieves a specific page
News news = NewsProvider.GetNews(nodeGuid, "en-US", "MySite");

// Accesses the 'Title' field
string title = news.Fields.Title;

// Accesses the 'Text' field
string text = news.Fields.Text;

Resolving HTML tags and relative URLs

Fields which are populated by the Rich text editor form control may contain HTML tags and relative links. To ensure that the content is displayed correctly, use one of the following methods in your views:

  • The standard Html.Raw method, which disables HTML encoding for the values.
  • The Html.Kentico().ResolveUrls extension method, which disables HTML encoding for the values and additionally resolves relative URLs to their absolute format. By default, relative URLs are automatically resolved by a filter that processes the output of all pages. You can use the ResolveUrls method to minimize the output filtering requirements or if you wish to disable the filter completely (via the CMSMVCResolveRelativeUrls web.config key).
@Html.Raw(Model.<Rich text field content>)
@Html.Kentico().ResolveUrls(Model.<Rich text field content>)

Accessing page metadata

Page metadata are stored the same way as page fields data (in the TreeNode object), but are not provided in the generated classes. This means that you need to use the TreeNode.TryGetValue method to access page metadata.

All the metadata fields you can access are defined in the CMS_Document database table.

// Retrieves a specific page
News page = NewsProvider.GetNews(nodeGuid, "en-US", "MySite");

// Accesses the 'DocumentPageTitle' metadata field
page.TryGetValue("DocumentPageTitle", out object title);

// Accesses the 'DocumentPageKeywords' metadata field
page.TryGetValue("DocumentPageKeywords", out object keywords);

Was this page helpful?