Page Service to set Title, Headers and Activate Features
In 2sxc 12.02 we introduced the IPageService
which helps you to do things like
- Set the page Title, Keywords, Description or Base-Tag
- Add Meta-headers
- Add Icons or Icon-Sets
- Create JsonLD headers
- Activate page features like turnOn
We have an rich series of Razor tutorials. You should really check them out 👍.
Concept
A Razor template is standalone - the page requests that it's rendered, but there is no official way to also tell the page that the title should change or that certain headers must be set.
We approached it in a futuristic way because we wanted to make sure that the code you write works in Dnn and Oqtane. So here's how it works
- Your code will get a
IPageService
object using `GetService<IPageService>() - Your code can then tell it what you need - like
SetTitle(...)
orAddJsonLd(...)
- Once the Razor is done, the engine will forward these requests to the page and ensure it happens
👉 Read about Dependency Injection 👉 Read about the IPageService in the API docs
Set Page Properties
You can set these page properties
SetTitle(newToPrefix)
SetTitle(newTitle, placeholder)
see docsSetDescription(newDescription)
SetDescription(newDescription, placeholder)
see docsSetKeywords(additionalKeywords)
SetKeywords(additionalKeywords, placeholder)
see docsSetBase(url)
- see docs
Note that SetTitle, SetDescription and SetKeywords will prepend or append whatever you give them to the existing value, unless you specify a placeholder - in which case that placeholder will be replaced.
Set Http Response Codes
In some cases an App may need to set the page to 404 - so that google doesn't index it. Do this using
SetHttpStatus(statusCode, optionalmessage)
- see docs
Note: ATM this is Dnn ☢️ only, as Oqtane doesn't render each page at a time - so a status code wouldn't work there (yet).
Add Icons and Icon-Sets like FavIcon
Icons / FavIcons are minor enhancements to a page, but they serve more than just the icon in the browser tab. In many cases they may also have larger images used for tiles and more. Use these two commands to get them working:
AddIcon
- see docs to add a single iconAddIconSet
- see docs to add a bundle of icons for various use cases in one step
Add Custom Headers
Custom headers may be important for JavaScripts running on your page or other special purposes. Set them using these methods:
Add JsonLD Headers (Schema.org)
JsonLD is a powerful way to describe your page to other systems, especially web crawlers like Google. It uses conventions from schema.org. Use this to add custom JsonLD headers:
Add OpenGraph Headers
OpenGraph is a standard to describe your page for Facebook, Twitter and other more social-style systems.
It adds meta headers using og:
prefixes. Use these commands to add such headers:
AddOpenGraph(propertyName, content)
- see docs
Activate Page Features
👉 See Page Service to Activate Features & WebResources (JS, CSS, etc.)
We have an rich series of Razor tutorials. You should really check them out 👍.
Activate JavaScript with TurnOn (new v15)
TurnOn is a simple JavaScript helper which allows to you turn on / boot a script when everything it needs has been loaded. You can do this by creating HTML manually, but in v15 we added a simple helper which makes it easier.
TurnOn(runCommand)
- see docsTurnOn(runCommand, require: object)
- see docsTurnOn(runCommand, data: object)
- see docsTurnOn(runCommand, require: object, data: object)
- see docsTurnOn(turnOnSpecs)
- see docs
History
- Introduced in 2sxc 12.02 to replace the previous static implementation using RazorBlade
- Enhanced in 12.05