Table of Contents

Interface IPageService

Namespace
ToSic.Sxc.Services
Assembly
ToSic.Sxc.dll

Make changes to the page - usually from Razor.

[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

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.).

string Activate(params string[] keys)

Parameters

keys string[]

One or more strings containing Page-Feature keys

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.Activate(...)

Activate(NoParamOrder, 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.).

string Activate(NoParamOrder noParamOrder = default, bool condition = true, params string[] features)

Parameters

noParamOrder NoParamOrder

see Convention: Named Parameters

condition bool

Condition to determine if activation should happen

features string[]

One or more strings containing Page-Feature keys

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.Activate(...)

Remarks

  • This overload with condition added in v15.03

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

string AddCsp(string name, params string[] values)

Parameters

name string
values string[]

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddCsp(...)

AddIcon(string, NoParamOrder, string, int, string)

Add an Icon header tag to the Page.

string AddIcon(string path, NoParamOrder noParamOrder = default, string rel = "", int size = 0, string type = null)

Parameters

path string

Path to the image/icon file

noParamOrder NoParamOrder

see Convention: Named Parameters

rel string

the rel-text, default is 'icon'. common terms are also 'shortcut icon' or 'apple-touch-icon'

size int

Will be used in size='#x#' tag; only relevant if you want to provide multiple separate sizes

type string

An optional type. If not provided, will be auto-detected from known types or remain empty

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddIcon(...)

AddIconSet(string, NoParamOrder, object, IEnumerable<string>, IEnumerable<int>)

Add a set of icons to the page

string AddIconSet(string path, NoParamOrder noParamOrder = default, object favicon = null, IEnumerable<string> rels = null, IEnumerable<int> sizes = null)

Parameters

path string

Path to the image/icon file

noParamOrder NoParamOrder

see Convention: Named Parameters

favicon object

path to favicon, default is '/favicon.ico'

rels IEnumerable<string>
sizes IEnumerable<int>

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddIconSet(...)

AddJsonLd(object)

string AddJsonLd(object jsonObject)

Parameters

jsonObject object

A object which will be converted to JSON. We recommend using dictionaries to build the object.

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddJsonLd(...)

AddJsonLd(string)

string AddJsonLd(string jsonString)

Parameters

jsonString string

A prepared JSON string

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddJsonLd(...)

AddMeta(string, string)

Add a standard meta header tag. You may also want to use AddOpenGraph(string, string) or AddJsonLd(string)

string AddMeta(string name, string content)

Parameters

name string
content string

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddMeta(...)

AddOpenGraph(string, string)

Add an open-graph header according to http://ogp.me/

string AddOpenGraph(string property, string content)

Parameters

property string

Open Graph property name, like title or image:width. 'og:' is automatically prefixed if not included

content string

value of this property

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddOpenGraph(...)

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

string AddToHead(string tag)

Parameters

tag string

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddToHead(...)

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

string AddToHead(IHtmlTag tag)

Parameters

tag IHtmlTag

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.AddToHead(...)

AssetAttributes(NoParamOrder, 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

IRawHtmlString AssetAttributes(NoParamOrder noParamOrder = default, bool optimize = true, int priority = 0, string position = null, bool whitelist = true)

Parameters

noParamOrder NoParamOrder

see Convention: Named Parameters

optimize bool

Activate optimize, default is true

priority int

Optional priority of optimization. Must be more than 100 to have an effect.

position string

Optional position of the resource (head, body, bottom)

whitelist bool

Automatically add to CSP-whitelist. This uses a random key to protect against XSS.

Returns

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.

string SetBase(string url = null)

Parameters

url string

the optional url for the base tag - if null, will try to default to the real url for the current page

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.SetBase(...)

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)

string SetDescription(string value, string placeholder = null)

Parameters

value string
placeholder string

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.SetDescription(...)

SetHttpStatus(int, string)

Set the page status code if possible (it will work in DNN, but probably not in Oqtane)

string SetHttpStatus(int statusCode, string message = null)

Parameters

statusCode int

An HTTP status code like 404

message string

Message / Description text (optional) which would be included in the header

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.SetHttpStatus(...)

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)

string SetKeywords(string value, string placeholder = null)

Parameters

value string
placeholder string

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.SetKeywords(...)

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 like SetTitle('[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`
string SetTitle(string value, string placeholder = null)

Parameters

value string
placeholder string

Returns

string

Empty string, so it can be used on inline razor such as @Kit.Page.SetTitle(...)

TurnOn(object, NoParamOrder, object, object, IEnumerable<object>, bool, bool?, string)

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.

string TurnOn(object runOrSpecs, NoParamOrder noParamOrder = default, object require = null, object data = null, IEnumerable<object> args = null, bool condition = true, bool? noDuplicates = null, string addContext = null)

Parameters

runOrSpecs object
  • either a run string like window.myObject.myJs() (must always start with window)
  • or an object containing all the parameters which turnOn requires
noParamOrder NoParamOrder

see Convention: Named Parameters

require object

optional One or more requirements which must be met before the code starts. Can be one or many values and/or functions.

  • a string such as window.myObject or window.myObject.readyToStart()
  • an array of such strings
data object

optional any value such as a string, or an object - to pass into the run-command

args IEnumerable<dynamic>

optional array of values to pass to the run function (new v18.00)

condition bool

optional condition when this should happen - if false, it won't add anything (new v16.02)

noDuplicates bool?

Will not add this turnOn if an identical one is already added to the page (new 16.05)

addContext string

optional when args is used, the context is usually not needed or given. If you do need it, it can be merged with data (if that's also provided and is an object - so use data) or added to the end of the args list end. (new v18.00)

Returns

string

An empty string, just so you can use it directly in Razor like @Kit.Page.TurnOn("...")

Remarks

  • Added in v15.x
  • condition added in 16.02
  • noDuplicates added in 16.05
  • args and addContext added in v18.0