Table of Contents

Class RazorComponent

Namespace
ToSic.Sxc.Dnn
Assembly
ToSic.Sxc.Dnn.Razor.dll

The base class for Razor-Components in 2sxc 10+ to 2sxc 11 - deprecated now
Provides context infos like the Dnn object, helpers like Edit and much more.

To use this, create cshtml files like _person-list.cshtml in your app-folder. By default, they will be typed the old way - which continues to work for compatibility. We recommend to use this from now on. To do it, your code file must begin with an @inherits statement, like this:

Tip

We highly recommend you use @inherits Custom.Hybrid.Razor14 or @inherits Custom.Dnn.Razor12 instead

@inherits ToSic.Sxc.Dnn.RazorComponent

<h1>hello from RazorComponent</h1>

[PublicApi("...but deprecated! use Razor14, RazorTyped or newer")]
public abstract class RazorComponent : RazorComponentBase, IDnnDynamicCode, IHasLog, IHasDnn
Inheritance
object
WebPageExecutingBase
WebPageRenderingBase
WebPageBase
RazorComponentBase
RazorComponent
Implements

Fields

NotImplementedUseCustomBase

public const string NotImplementedUseCustomBase = "Use a newer base class like Custom.Hybrid.Razor12 or Custom.Dnn.Razor12 to leverage this."

Field Value

string

Properties

App

A fully prepared IApp object letting you access all the data and queries in the current app.

You'll usually want to access the data, like App.Data["Categories"] or the queries App.Query["AllPosts"].

foreach(var cat in AsList(App.Data["Categories"])) {
	@cat.Name
}
public IApp App { get; }

Property Value

IApp

The current app

CmsContext

This Context tells you about the environment, such as

  • the current User
  • the Page
  • the View
  • the Site

It's supposed to replace direct access to Dnn or Oqtane object in Razor and WebAPI code, allowing hybrid code that works everywhere.

public ICmsContext CmsContext { get; }

Property Value

ICmsContext

Remarks

New in v11.11

Code

Code-Behind of this .cshtml file - located in a file with the same name but ending in .code.cshtml

public dynamic Code { get; }

Property Value

dynamic

Content

The content object of the current razor view - IF the current view has content. If the view is a list, it will return the first item. Will be null otherwise. To tell if it's the demo/default item, use IsDemoItem.

public dynamic Content { get; }

Property Value

dynamic

A IDynamicEntity object with the current content - or null.

Dnn

The DNN context. Has various objects to access the Dnn Page, etc.

public IDnnContext Dnn { get; }

Property Value

IDnnContext

Edit

Helper commands to enable in-page editing functionality Use it to check if edit is enabled, generate context-json infos and provide toolbar buttons

public IEditService Edit { get; }

Property Value

IEditService

An IEditService object.

Header

The header object of the current razor view, if it's a list and has a header object. If it's a list and doesn't have a header (and no default), it will return null. To tell if it's the demo/default item, use IsDemoItem.

public dynamic Header { get; }

Property Value

dynamic

A IDynamicEntity object with the current content.

Remarks

Introduced in 2sxc 10.10 - previously it was called ListContent, now deprecated.

Html

Helper for Html.Raw - for creating raw html output which doesn't encode > and <. Also has helpers such as .Partial(...)

public override IHtmlHelper Html { get; }

Property Value

IHtmlHelper

Link helper object to create the correct links

public ILinkService Link { get; }

Property Value

ILinkService

A ILinkService object.

Log

The logger for the current Razor / WebApi which allows you to add logs to Insights.

public override ICodeLog Log { get; }

Property Value

ICodeLog

Purpose

The purpose of the current execution. The code might be called for showing to a user, or search-indexing.

[Obsolete("Shouldn't be used any more, but will continue to work for indefinitely for old base classes, not in v12. There are now better ways of doing this")]
public Purpose Purpose { get; }

Property Value

Purpose

The value of the current purpose.

Methods

AsAdam(ICanBeEntity, string)

Provides an Adam instance for this item and field

public IFolder AsAdam(ICanBeEntity item, string fieldName)

Parameters

item ICanBeEntity

The item - an IEntity, IDynamicEntity, ITypedItem etc. often Content or similar

fieldName string

The field name, like "Gallery" or "Pics"

Returns

IFolder

An Adam object for navigating the assets

AsDynamic(object)

Take a json and provide it as a dynamic object to the code

public dynamic AsDynamic(object dynamicEntity)

Parameters

dynamicEntity object

Returns

dynamic

A dynamic object representing the original json. If it can't be parsed, it will parse the fallback, which by default is an empty empty dynamic object. If you provide null for the fallback, then you will get null back.

Remarks

Added in 2sxc 10.22.00

AsDynamic(string, string)

Take a json and provide it as a dynamic object to the code

public dynamic AsDynamic(string json, string fallback = null)

Parameters

json string

the original json string

fallback string

Alternate string to use, if the original json can't parse. Can also be null or the word "error" if you would prefer an error to be thrown.

Returns

dynamic

A dynamic object representing the original json. If it can't be parsed, it will parse the fallback, which by default is an empty empty dynamic object. If you provide null for the fallback, then you will get null back.

Remarks

Added in 2sxc 10.22.00

AsDynamic(IEntity)

Wraps an entity into a IDynamicEntity

public dynamic AsDynamic(IEntity entity)

Parameters

entity IEntity

the original object

Returns

dynamic

a dynamic object for easier coding

AsEntity(object)

Unwraps a dynamic entity or ITypedItem back into the underlying IEntity

public IEntity AsEntity(object dynamicEntity)

Parameters

dynamicEntity object

the wrapped IEntity

Returns

IEntity

A normal IEntity

AsList(object)

Converts a list of IEntity objects into a list of IDynamicEntity objects.

public IEnumerable<dynamic> AsList(object list)

Parameters

list object

typically a List/IEnumerable of Entities or DynamicEntities.
Can also be a IDataSource in which case it uses the default stream.

Returns

IEnumerable<dynamic>

a list of IDynamicEntity objects

Remarks

Added in 2sxc 10.21.00

CreateInstance(string, NoParamOrder, string, string, bool)

Create an instance of code lying in a file near this

public virtual dynamic CreateInstance(string virtualPath, NoParamOrder noParamOrder = default, string name = null, string relativePath = null, bool throwOnError = true)

Parameters

virtualPath string

path to the other code file to compile

noParamOrder NoParamOrder

see Convention: Named Parameters

name string

Override the class name to compile - usually not required as it should match the file name

relativePath string

optional relative path, will usually use the ToSic.Sxc.Code.Internal.IGetCodePath.CreateInstancePath

throwOnError bool

throw errors if compiling fails, recommended

Returns

dynamic

An object of the class in the file

Remarks

Note that the C# code which we are creating inherits from a standard base class such as Code12 or ToSic.Sxc.Code.DynamicCode then it will automatically be initialized to support App, AsDynamic etc.

CreateSource<T>(IDataSource, ILookUpEngine)

Create a IDataSource which will process data from the given stream.

public T CreateSource<T>(IDataSource inSource = null, ILookUpEngine configurationProvider = null) where T : IDataSource

Parameters

inSource IDataSource

The data source which will be the default In of the new data-source.

configurationProvider ILookUpEngine

An alternate configuration provider for the DataSource

Returns

T

A typed DataSource object

Type Parameters

T

A data-source type - must be inherited from IDataSource

CreateSource<T>(IDataStream)

Create a IDataSource which will process data from the given stream.

public T CreateSource<T>(IDataStream source) where T : IDataSource

Parameters

source IDataStream

The stream which will be the default In of the new data-source.

Returns

T

A typed DataSource object

Type Parameters

T

A data-source type - must be inherited from IDataSource

CustomizeData()

Override this to have your code change the (already initialized) Data object. If you don't override this, nothing will be changed/customized.

[Obsolete("Shouldn't be used any more, but will continue to work for indefinitely for old base classes, not in v12. There are now better ways of doing this")]
public virtual void CustomizeData()

CustomizeSearch(Dictionary<string, List<ISearchItem>>, IModule, DateTime)

Customize how the search will process data on this page.

[Obsolete("Shouldn't be used any more, but will continue to work for indefinitely for old base classes, not in v12. There are now better ways of doing this")]
public virtual void CustomizeSearch(Dictionary<string, List<ISearchItem>> searchInfos, IModule moduleInfo, DateTime beginDate)

Parameters

searchInfos Dictionary<string, List<ISearchItem>>
moduleInfo IModule
beginDate DateTime

GetService<TService>()

Get a service from the Dependency Injection. The service can come from 2sxc, EAV or the underlying platform (Dnn, Oqtane).

public TService GetService<TService>() where TService : class

Returns

TService

An object of the type or interface requested, or null if not found in the DI.

Type Parameters

TService

Interface (preferred) or Class which is needed