Interface IPageService
Make changes to the page - usually from Razor.
Namespace: ToSic.Sxc.Services
Assembly: ToSic.Sxc.dll
Syntax
[PublicApi]
public interface IPageService
Remarks
History
- Introduced in v12.02 but on another namespace which still works for compatibility
- Moved to ToSic.Sxc.Services in v13
- Added ability to use placeholder
[original]
in v13.11 - Most commands were updated to return an empty string in v14.02 so that they could be used as inline razor (previously
void
)
Methods
| Improve this Doc View SourceActivate(string, bool, params string[])
Activate a feature on this page, such as turnOn
, 2sxc.JsCore
etc.
For list of features, see Page Service to Activate Features & WebResources (JS, CSS, etc.).
Declaration
string Activate(string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", bool condition = true, params string[] features)
Parameters
Type | Name | Description |
---|---|---|
string | noParamOrder | |
bool | condition | Condition to determine if activation should happen |
string[] | features | One or more strings containing Page-Feature keys |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
Remarks
- This overload with
condition
added in v15.03
Activate(params string[])
Activate a feature on this page, such as turnOn
, 2sxc.JsCore
etc.
For list of features, see Page Service to Activate Features & WebResources (JS, CSS, etc.).
Declaration
string Activate(params string[] keys)
Parameters
Type | Name | Description |
---|---|---|
string[] | keys | One or more strings containing Page-Feature keys |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddCsp(string, params string[])
Add a CSP rule where you also specify the name. Best check the CSP Guide.
For an example, see Coded CSP
Declaration
string AddCsp(string name, params string[] values)
Parameters
Type | Name | Description |
---|---|---|
string | name | |
string[] | values |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddIcon(string, string, string, int, string)
Add an Icon header tag to the Page.
Declaration
string AddIcon(string path, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", string rel = "", int size = 0, string type = null)
Parameters
Type | Name | Description |
---|---|---|
string | path | Path to the image/icon file |
string | noParamOrder | |
string | rel | the rel-text, default is 'icon'. common terms are also 'shortcut icon' or 'apple-touch-icon' |
int | size | Will be used in size='#x#' tag; only relevant if you want to provide multiple separate sizes |
string | type | An optional type. If not provided, will be auto-detected from known types or remain empty |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddIconSet(string, string, object, IEnumerable<string>, IEnumerable<int>)
Add a set of icons to the page
Declaration
string AddIconSet(string path, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", object favicon = null, IEnumerable<string> rels = null, IEnumerable<int> sizes = null)
Parameters
Type | Name | Description |
---|---|---|
string | path | Path to the image/icon file |
string | noParamOrder | |
object | favicon | path to favicon, default is '/favicon.ico' |
IEnumerable<string> | rels | |
IEnumerable<int> | sizes |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddJsonLd(object)
Add a JSON-LD header according https://developers.google.com/search/docs/guides/intro-structured-data
Declaration
string AddJsonLd(object jsonObject)
Parameters
Type | Name | Description |
---|---|---|
object | jsonObject | A object which will be converted to JSON. We recommend using dictionaries to build the object. |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddJsonLd(string)
Add a JSON-LD header according https://developers.google.com/search/docs/guides/intro-structured-data
Declaration
string AddJsonLd(string jsonString)
Parameters
Type | Name | Description |
---|---|---|
string | jsonString | A prepared JSON string |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddMeta(string, string)
Add a standard meta header tag. You may also want to use AddOpenGraph(string, string) or AddJsonLd(string)
Declaration
string AddMeta(string name, string content)
Parameters
Type | Name | Description |
---|---|---|
string | name | |
string | content |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddOpenGraph(string, string)
Add an open-graph header according to http://ogp.me/
Declaration
string AddOpenGraph(string property, string content)
Parameters
Type | Name | Description |
---|---|---|
string | property | Open Graph property name, like title or image:width. 'og:' is automatically prefixed if not included |
string | content | value of this property |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddToHead(string)
Add a tag to the header of the page Will simply not do anything if an error occurs, like if the page object doesn't exist
Declaration
string AddToHead(string tag)
Parameters
Type | Name | Description |
---|---|---|
string | tag |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AddToHead(IHtmlTag)
Add a RazorBlade Tag to the headers of the page Will simply not do anything if an error occurs, like if the page object doesn't exist
Declaration
string AddToHead(IHtmlTag tag)
Parameters
Type | Name | Description |
---|---|---|
IHtmlTag | tag |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
AssetAttributes(string, bool, int, string, bool)
Add common html attributes to a script
or link
tag to enable optimizations
and automatically whitelist in the Content Security Policy
Declaration
IRawHtmlString AssetAttributes(string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", bool optimize = true, int priority = 0, string position = null, bool whitelist = true)
Parameters
Type | Name | Description |
---|---|---|
string | noParamOrder | |
bool | optimize | Activate optimize, default is true |
int | priority | Optional priority of optimization. Must be more than 100 to have an effect. |
string | position | Optional position of the resource ( |
bool | whitelist | Automatically add to CSP-whitelist. This uses a random key to protect against XSS. |
Returns
Type | Description |
---|---|
IRawHtmlString | The asset attributes in a format which will be preserved in HTML |
Remarks
History: Created in 2sxc 13.10
SetBase(string)
Add a standard base header tag or replace it if one is already provided.
Declaration
string SetBase(string url = null)
Parameters
Type | Name | Description |
---|---|---|
string | url | the optional url for the base tag - if null, will try to default to the real url for the current page |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
SetDescription(string, string)
Set the Page Description.
It will either try to replace the placeholder (second parameter)
or prefix it to the existing description (unless [original]
is given).
See also the details with placeholder or [original]
as explained on SetTitle(string, string)
Declaration
string SetDescription(string value, string placeholder = null)
Parameters
Type | Name | Description |
---|---|---|
string | value | |
string | placeholder |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
SetHttpStatus(int, string)
Set the page status code if possible (it will work in DNN, but probably not in Oqtane)
Declaration
string SetHttpStatus(int statusCode, string message = null)
Parameters
Type | Name | Description |
---|---|---|
int | statusCode | An HTTP status code like 404 |
string | message | Message / Description text (optional) which would be included in the header |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
SetKeywords(string, string)
Set the Page Keywords.
It will either try to replace the placeholder (second parameter)
or prefix it to the existing keywords (unless [original]
is given).
See also the details with placeholder or [original]
as explained on SetTitle(string, string)
Declaration
string SetKeywords(string value, string placeholder = null)
Parameters
Type | Name | Description |
---|---|---|
string | value | |
string | placeholder |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
SetTitle(string, string)
Set the Page Title. Behavior:
- By default it will prefix the new title -
SetTitle('My New Title - ')
=My New Title - Blog - 2sxc.org
- You can also use the new
[original]
token likeSetTitle('[original] - My New Title')
=Blog - 2sxc.org - My New Title
- You can add a placeholder to the page-title and tell SetTitle what it is.
SetTitle('My New Title', '2sxc.org') =
Blog - My New Title`
Declaration
string SetTitle(string value, string placeholder = null)
Parameters
Type | Name | Description |
---|---|---|
string | value | |
string | placeholder |
Returns
Type | Description |
---|---|
string | Empty string, so it can be used on inline razor such as |
TurnOn(object, string, object, object, bool, bool?)
Turn on some javascript code when all requirements have been met. Uses turnOn.
Will automatically activate the feature and set hidden data on the page for the turnOn JS to pick up.
Declaration
string TurnOn(object runOrSpecs, string noParamOrder = "Params must be named (https://go.2sxc.org/named-params)", object require = null, object data = null, bool condition = true, bool? noDuplicates = null)
Parameters
Type | Name | Description |
---|---|---|
object | runOrSpecs |
|
string | noParamOrder | |
object | require | optional One or more requirements which must be met before the code starts. Can be one or many values and/or functions.
|
object | data | optional any value such as a string, or an object - to pass into the run-command |
bool | condition | optional condition when this should happen - if false, it won't add anything (new v16.02) |
bool? | noDuplicates | Will not add this turnOn if an identical one is already added to the page (new 16.05) |
Returns
Type | Description |
---|---|
string | An empty string, just so you can use it directly in Razor like |
Remarks
- Added in v15.x
condition
added in 16.02noDuplicates
added in 16.05