Displaying page content

Page content is stored in the database and edited through the administration interface using the content tree in the Pages application. To display the content on your site, you need to retrieve the data in the code of your MVC application.

The implementation and complexity of the code depends on your preferences and project requirements, ranging from simple method calls within controller actions to custom solutions utilizing repository and service patterns. You can then use the retrieved page content to populate prepared view models and display them in your views.

Retrieving page data

To retrieve page content, use the IPageRetriever service, together with generated classes representing specific page types. The generated page type classes allow you to work with strongly typed page objects and easily access their fields. The following example demonstrates the retrieval of a sample Article page type:

 private readonly IPageRetriever pageRetriever; // Gets an instance of the IPageRetriever service using dependency injection public ExampleController(IPageRetriever pageRetriever) < this.pageRetriever = pageRetriever; >public ActionResult Index() < // Retrieves pages of 'Article' page type that are in the '/Articles/May' section of the content tree var articles = pageRetriever.Retrieve( query => query .Path("/Articles/May", PathTypeEnum.Children)); > 

To filter the retrieved pages, use the methods listed in Reference - DocumentQuery methods.

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

Selecting content based on page taxonomy

Pages on Xperience websites can be categorized and organized in several different ways. The code that you use to retrieve content from specific “categories” depends on the taxonomy approach used by the given pages:

 // Gets the articles contained directly under the '/Articles/Featured' section of the content tree var articles = pageRetriever.Retrieve( query => query .Path("/Articles/Featured/", PathTypeEnum.Children) .NestingLevel(1)); 
 // Gets the article pages assigned to the 'Featured' category var articles = pageRetriever.Retrieve( query => query .InCategories("Featured")); 
 // Gets the articles marked with the 'Coffee' tag from the 'Beverages' tag group var articles = pageRetriever.Retrieve( query => query .WithTag("Coffee", "Beverages")); 

Working with retrieved page data

You can access various types of data from retrieved page objects:

Example
 // Retrieves a specific page var article = pageRetriever.Retrieve(query => query .WithGuid(nodeGuid)) .FirstOrDefault(); // Accesses the ID of the page's parent node in the content tree int parentNodeId = article.NodeParentID; // Accesses the 'DocumentName' data field string pageName = article.DocumentName; 

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:

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

 // Retrieves a specific page var article = pageRetriever.Retrieve(query => query .WithGuid(nodeGuid)) .FirstOrDefault(); // Accesses the 'Title' field string title = article.Fields.Title; // Accesses the 'Text' field string text = article.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:

 @Html.Raw(Model.) @Html.Kentico().ResolveUrls(Model.) 

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.

Getting page URLs

Pages in the content tree of Xperience websites can have their live site URLs generated by the system, either through content tree-based routing or according to URL patterns set for page types. This allows content editors to create new pages or adjust the URLs of existing ones in the administration interface, without the need to update and redeploy the code of the MVC application.

You may need to get the URLs of retrieved pages in various scenarios, such as rendering navigation menus or other page links, performing redirects, etc.

To get the URL of a page in general code:

  1. Obtain an instance of the IPageUrlRetriever service (for example through dependency injection in your controller constructor).
  2. Call the service’s Retrieve method, and use one of the following parameter types to specify the page:
    • TreeNode object representing the page (or a specific page type class inheriting from TreeNode).
    • string containing a node alias path value, representing the page’s location in the Xperience content tree

The IPageUrlRetriever.Retrieve method returns a PageUrl object, with properties containing the given page’s URL:

The URL is automatically adjusted for the current culture, i.e. any language prefixes in the URL path or the domain in the absolute URL (see Configuring URLs for multilingual websites to learn more).

Example
 using System.Web.Mvc; using CMS.DocumentEngine; using Kentico.Content.Web.Mvc; public class PageController : Controller < private readonly IPageUrlRetriever pageUrlRetriever; // Controller constructor that receives an IPageUrlRetriever instance using dependency injection public PageController(IPageUrlRetriever pageUrlRetriever) < this.pageUrlRetriever = pageUrlRetriever; >. // Gets the relative URL of a specified page (TreeNode object or inheriting page type class) string relativePageUrl = pageUrlRetriever.Retrieve(page).RelativePath; . > 

Page URL extension methods

In addition to the IPageUrlRetriever API, the system provides UrlHelper extension methods for getting page URLs.