Access context data
Overview
Sitefinity ASP.NET Core Renderer provides context data through the RequestContext
and IRenderContext
services.
IRequestContext
The IRequestContext
interface holds information regarding:
- Current user
- Current site
- Current language
- Page layout
- Current page
To access it in the ViewComponents
, you must inject it in the constructor.
For more information, see Access context information sample on Sitefinity GitHub repository.
Additionally, the current user object is synced to the httpContext.User
property and the CurrentLanguage
property is synced with the Thread.CurrentUICulture
property.
Current user
The current user profile is exposed through a web method and the custom fields assigned to it are also available. The current user profile returns the following fields:
- Short text
- Long text
- Yes / No
- Date and Time
- Number
- Taxonomies
Current language
The main purpose of the current language property is to hold the language version of the page that is currently being rendered.
You can use it in the following cases:
- To localize content
For more information, see Localization.
- To build a language selector.
For more information, see the Language selector widget sample on Sitefinity GitHub repository.
Current page
The current page object is provided in the IRequestContext
interface, containing all fields that are not related data fields.
You can access them by injecting the IRequestContext
object through a dependancy injection and accessing the fields with the GetValue
method that is part of Sitefinity REST SDK.
For more information, see REST SDK » The SdkItem class and ISdkItem interface on Sitefinity GitHub repository.
If you want to fetch and access related data items of the page object, you must make a query to Sitefinity CMS using the REST SDK.
For more information, see Default.cshtml of the Accessing context information sample on Sitefinity GitHub Repository.
Current site
You can use the current site object for contextual information, depending on the requirements of the widgets. One such example is the Language selector widget, linked above.
For more information, see the Language selector widget sample on Sitefinity GitHub repository.
IRenderContext
The IRenderContext
interface holds two properties and one method:
IsEdit
Used to check whether the current request is executing in the page editor.
IsPreview
Used to check whether the current request is executing in the page preview.
IsLive()
Used to check whether the current request is executing when browsing the live site.
These properties are useful for showing contextual information in edit mode, when an actual representation of the widget cannot be rendered.
For more information, see the Conditional rendering in editor sample on Sitefinity GitHub repository.