Class Api12
This is the base class for all custom API Controllers.
With this, your code receives the full context incl. the current App, DNN, Data, etc.
[PublicApi("This is the official base class for v12+")]
public abstract class Api12 : DnnSxcCustomControllerBase, IHasLog
- Inheritance
-
objectApiControllerDnnApiControllerDnnSxcControllerRootDnnSxcControllerBaseDnnSxcCustomControllerBaseApi12
- Implements
- Derived
Constructors
Api12()
protected Api12()
Api12(string)
protected Api12(string logSuffix)
Parameters
logSuffixstring
Properties
App
A fully prepared IApp object letting you access all the data and queries in the current app.
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
Remarks
New in v11.11
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.
Convert
Conversion helper for common data conversions in Razor and WebAPIs
public IConvertService Convert { get; }
Property Value
Remarks
Added in 2sxc 12.05
Data
The data prepared for the current Code. Usually user data which was manually added to the instance, but can also be a query.
public IDataSource Data { get; }
Property Value
- IDataSource
A standard IDataSource.
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.
Link
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 ICodeLog Log { get; }
Property Value
Resources
Resources for this Scenario. This is a dynamic object based on the IDynamicStack.
It will combine both the Resources of the View and the App. The View-Resources will have priority. In future it may also include some global Resources.
🪒 Use in Razor: @Resources.CtaButtonLabel
public dynamic Resources { get; }
Property Value
- dynamic
Remarks
New in 12.03
Settings
Settings for this Scenario. This is a dynamic object based on the IDynamicStack.
It will combine both the Settings of the View and the App. The View-Settings will have priority. In future it may also include some global Settings.
🪒 Use in Razor: @Settings.ItemsPerRow
public dynamic Settings { get; }
Property Value
- dynamic
Remarks
New in 12.03
Methods
Accepted()
Creates a .net-core like AcceptedResult object that produces an .net-core like StatusCodes.Status202Accepted response.
Typical use: return Accepted();
[NonAction]
public dynamic Accepted()
Returns
- dynamic
The created .net-core like
AcceptedResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
AsAdam(ICanBeEntity, string)
Provides an Adam instance for this item and field
public IFolder AsAdam(ICanBeEntity item, string fieldName)
Parameters
itemICanBeEntityThe item - an IEntity, IDynamicEntity, ITypedItem etc. often Content or similar
fieldNamestringThe field name, like "Gallery" or "Pics"
Returns
- IFolder
An Adam object for navigating the assets
AsDynamic(object)
Convert a dynamic entity and return itself again. This is so coders don't have to worry if the original object was an IEntity or a IDynamicEntity in the first place.
public dynamic AsDynamic(object dynamicEntity)
Parameters
dynamicEntityobjectthe original object
Returns
- dynamic
a dynamic object for easier coding
AsDynamic(params object[])
Convert one or many Entities and Dynamic entities into an IDynamicStack
public dynamic AsDynamic(params object[] entities)
Parameters
entitiesobject[]one or more source object
Returns
- dynamic
a dynamic object for easier coding
Remarks
New in 12.05
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
jsonstringthe original json string
fallbackstringAlternate 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
entityIEntitythe 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
dynamicEntityobjectthe 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
listobjecttypically 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
BadRequest()
Creates an .net-core like BadRequestResult that produces a .net-core like StatusCodes.Status400BadRequest response.
Typical use: return BadRequest();
[NonAction]
public dynamic BadRequest()
Returns
- dynamic
The created .net-core like
BadRequestResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
Conflict()
Creates an .net-core like ConflictResult that produces a .net-core like StatusCodes.Status409Conflict response.
Typical use: return Conflict();
[NonAction]
public dynamic Conflict()
Returns
- dynamic
The created .net-core like
ConflictResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
Conflict(object)
Creates an .net-core like ConflictObjectResult that produces a .net-core like StatusCodes.Status409Conflict response.
Typical use: return Conflict("the stored file is newer");
[NonAction]
public dynamic Conflict(object error)
Parameters
errorobjectContains errors to be returned to the client.
Returns
- dynamic
The created .net-core like
ConflictObjectResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
CreateInstance(string, NoParamOrder, string, string, bool)
Create an instance of code lying in a file near this
public dynamic CreateInstance(string virtualPath, NoParamOrder noParamOrder = default, string name = null, string relativePath = null, bool throwOnError = true)
Parameters
virtualPathstringpath to the other code file to compile
noParamOrderNoParamOrdernamestringOverride the class name to compile - usually not required as it should match the file name
relativePathstringoptional relative path, will usually use the ToSic.Sxc.Code.Internal.IGetCodePath.CreateInstancePath
throwOnErrorboolthrow 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
inSourceIDataSourceThe data source which will be the default In of the new data-source.
configurationProviderILookUpEngineAn alternate configuration provider for the DataSource
Returns
- T
A typed DataSource object
Type Parameters
TA 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
sourceIDataStreamThe stream which will be the default In of the new data-source.
Returns
- T
A typed DataSource object
Type Parameters
TA data-source type - must be inherited from IDataSource
File(NoParamOrder, bool?, string, string, string, object)
Create a File-result to stream to the client
Typical use: return File(download: true, contentType: "text/xml", contents: ...);
public dynamic File(NoParamOrder noParamOrder = default, bool? download = null, string virtualPath = null, string contentType = null, string fileDownloadName = null, object contents = null)
Parameters
noParamOrderNoParamOrderdownloadbool?If a download should be enforced (otherwise the file may just be displayed - like an image)
virtualPathstringPath in the website to get the file from. Provide either virtualPath or contents
contentTypestringMime Content-type. Will try to auto-detect from virtualPath or fileDownloadName if not provided.
fileDownloadNamestringDownload name. If provided, it will try to force download/save on the browser.
contentsobjectContent of the result - a string, byte[] or stream to include.
Returns
- dynamic
Remarks
Added in 2sxc 12.05
Forbid()
Creates a .net-core like ForbidResult (.net-core like StatusCodes.Status403Forbidden by default).
Typical use: return Forbid();
[NonAction]
public dynamic Forbid()
Returns
- dynamic
The created .net-core like
ForbidResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
Some authentication schemes, such as cookies, will convert .net-core like StatusCodes.Status403Forbidden to
a redirect to show a login page.
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
TServiceInterface (preferred) or Class which is needed
NoContent()
Creates a .net-core like NoContentResult object that produces an empty
.net-core like StatusCodes.Status204NoContent response.
Typical use: return NoContent();
[NonAction]
public dynamic NoContent()
Returns
- dynamic
The created .net-core like
NoContentResultobject for the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
NotFound()
Creates an .net-core like NotFoundResult that produces a .net-core like StatusCodes.Status404NotFound response.
Typical use: return NotFound();
[NonAction]
public dynamic NotFound()
Returns
- dynamic
The created .net-core like
NotFoundResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
NotFound(object)
Creates an .net-core like NotFoundObjectResult that produces a .net-core like StatusCodes.Status404NotFound response.
Typical use: return Unauthorized("try another ID");
[NonAction]
public dynamic NotFound(object value)
Parameters
valueobject
Returns
- dynamic
The created .net-core like
NotFoundObjectResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
Ok()
Creates a .net-core like OkResult object that produces an empty .net-core like StatusCodes.Status200OK response.
Typical use: return Ok();
[NonAction]
public dynamic Ok()
Returns
- dynamic
The created .net-core like
OkResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
Ok(object)
Creates an .net-core like OkObjectResult object that produces an .net-core like StatusCodes.Status200OK response.
Typical use: return Ok(objectToInclude);
[NonAction]
public dynamic Ok(object value)
Parameters
valueobjectThe content value to format in the entity body.
Returns
- dynamic
The created .net-core like
OkObjectResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
Redirect(string)
Creates a .net-core like RedirectResult object that redirects (.net-core like StatusCodes.Status302Found)
to the specified url.
Typical use: return Redirect("https://2sxc.org");
[NonAction]
public dynamic Redirect(string url)
Parameters
urlstringThe URL to redirect to.
Returns
- dynamic
The created .net-core like
RedirectResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
RedirectPermanent(string)
Creates a .net-core like RedirectResult object with .net-core like RedirectResult.Permanent set to true
(.net-core like StatusCodes.Status301MovedPermanently) using the specified url.
Typical use: return RedirectPermanent("https://2sxc.org");
[NonAction]
public dynamic RedirectPermanent(string url)
Parameters
urlstringThe URL to redirect to.
Returns
- dynamic
The created .net-core like
RedirectResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
SaveInAdam(NoParamOrder, Stream, string, string, Guid?, string, string)
Save a file from a stream (usually an upload from the browser) into an adam-field of an item. Read more about this in the WebAPI docs for SaveInAdam
public IFile SaveInAdam(NoParamOrder noParamOrder = default, Stream stream = null, string fileName = null, string contentType = null, Guid? guid = null, string field = null, string subFolder = "")
Parameters
noParamOrderNoParamOrderstreamStreamthe stream
fileNamestringfile name to save to
contentTypestringcontent-type of the target item (important for security checks)
guidGuid?fieldstringsubFolderstring
Returns
StatusCode(int)
Creates a .net-core like StatusCodeResult object by specifying a statusCode.
Typical use: return StatusCode(403);
[NonAction]
public dynamic StatusCode(int statusCode)
Parameters
statusCodeintThe status code to set on the response.
Returns
- dynamic
The created .net-core like
StatusCodeResultobject for the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
StatusCode(int, object)
Creates a .net-core like ObjectResult object by specifying a statusCode and value
Typical use: return StatusCode(304, "not modified");
[NonAction]
public dynamic StatusCode(int statusCode, object value)
Parameters
statusCodeintThe status code to set on the response.
valueobjectThe value to set on the .net-core like `ObjectResult"/>.
Returns
- dynamic
The created .net-core like
ObjectResultobject for the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
Unauthorized()
Creates an .net-core like UnauthorizedResult that produces an .net-core like StatusCodes.Status401Unauthorized response.
Typical use: return Unauthorized();
[NonAction]
public dynamic Unauthorized()
Returns
- dynamic
The created .net-core like
UnauthorizedResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.
Unauthorized(object)
Creates an .net-core like UnauthorizedObjectResult that produces a .net-core like StatusCodes.Status401Unauthorized response.
Typical use: return Unauthorized("we don't like this");
[NonAction]
public dynamic Unauthorized(object value)
Parameters
valueobject
Returns
- dynamic
The created .net-core like
UnauthorizedObjectResultfor the response.
Remarks
This is a shim to ensure that .net Framework code can be written the same way as .net core WebApis.
It returns a dynamic to make it easy to use, but the real .net core implementation returns a typed object.