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.
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.
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"));
You can access various types of data from retrieved page objects:
// 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;
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.
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:
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; . >
In addition to the IPageUrlRetriever API, the system provides UrlHelper extension methods for getting page URLs.
@using System.Web.Mvc @using Kentico.Content.Web.Mvc @using Kentico.Web.Mvc . @* Renders a link to the page under the '/Home' alias path in the site's content tree *@ @Html.HtmlLink(Url.Kentico().PageUrl("/Home"), "Home page"))
The methods return the page’s application absolute URL path, as a PageUrlString object. The object automatically converts to a string within views. You can call the PageUrlString object’s ToAbsolute() method to get the page’s absolute URL, including the schema and domain name.
Tip
For methods that accept node alias path parameters, you can use a helper class to store the paths to frequently targeted pages as constants. For example:
public static class ContentItemIdentifiers < public const string HOME = "/Home"; public const string ARTICLES = "/Articles"; >
Canonical URLs
The API also provides the Kentico().PageCanonicalUrl() extension method, which returns page URLs suitable for Canonical link elements. Rendering canonical links is recommended to improve SEO on websites where pages are available under multiple URLs, typically when using alternative URLs or linked pages.
The standard way of configuring SEO-related parameters for a website’s pages is to add elements into the section of the page HTML code. To utilize the metadata entered for pages in the administration interface, such as the page title and description, you can use the following HtmlHelper extension methods provided by the Xperience API:
The methods render the corresponding HTML elements for the currently displayed page. Before rendering the elements, all methods transform the metadata based on the settings for metadata format and prefixes (i.e. add a prefix or resolve macros in the content of the metadata).
To work, the methods must have the context of the current page available:
For the PageTitle method, you can optionally set the alternateTitle parameter, which is then used for pages where the title and context of the current page is not available (e.g. completely custom pages that are not represented in the Xperience content tree). For example, you can get the alternateTitle value from a ViewBag property used inside the view layout.
The following example of view code uses the extension methods to render metadata tags for the current page (such code can be added to the site’s main layout):
@using Kentico.Content.Web.Mvc @using Kentico.Web.Mvc .
General API for retrieving page metadata
You can also access the metadata for specific pages through the properties of IPageMetadata instances. To get the IPageMetadata instance for a page, use one of the following approaches (depending on your scenario and context):
Was this page helpful?
On this pageWe use small cookies to improve your browsing experience and to analyze traffic. To process the analytical cookies, we need your consent. You may revoke your consent on the Cookies Policy page or in your browser at any time.