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 Reference - DocumentQuery methods.

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 through the properties of the object. The following types of page data are available:

  • Page fields – the data that is editable on a page’s Content tab.
  • General page data – properties available for all pages (via the TreeNode class). For example identifiers, values indicating the position in the content tree, etc.
  • 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.
Example



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

// Accesses the ID of the page's parent node in the content tree
int parentNodeId = news.NodeParentID;

// Accesses the 'DocumentPageTitle' metadata field
string pageTitle = news.DocumentPageTitle;



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 corresponding <namespace>_<page_type_code_name> 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>)