App / @App object Dynamic Code
App object gives you full access to everything you need to know about the current App, including Path-info, access to all Data this App has, access to Settings and language Resources and more.
Discover More in the Razor TutorialsWe have an rich series of Razor tutorials. You should really check them out 👍.
How to use
Here's are two simple examples taken from the Blog App:
<script... use the app-path to ensure that the file is correctly loaded, no matter what portal or app-name is currently valid. Here you can discover more about the optimizations.
The loop iterates through all tags with the
@foreach(var tag in AsDynamic(App.Data["Tag"])), creates
<li> items and links these to a page defined in the
How it works
Whenever a 2sxc-instance is created to render a page or to deliver JSON data, the
App object is created and prepared to deliver everything you need. It's very performant, because it doesn't actually get any data or run any queries unless these are accessed.
The app-object uses the
IApp interface (see code) has the following simple properties:
AppIdnumber, current App id
AppGuidguid, internal use global id
ConfigurationDynamicEntity, contains the configuration content-item
- Data IAppData, to access all App-data (see below)
Folderstring, storage folder name in portal/#/2sxc/...
Hiddenbool, info if the app cannot be selected in the UIs
Namestring, the app name
Pathstring, the path as used in URLs in html
PhysicalPathstring, the path as used on the server C:...
- Query["QueryName"] dictionary of queries (see below)
ResourcesDynamicEntity, all the multi-language labels etc. (see below)
SettingsDynamicEntity, all the app-settings (see below)
ZoneIdnumber, current Zone ID (similar to PortalId)
Using App Data (App.Data)
Using App Queries (App.Query)
Note about Unpublished / Draft Content-Items
In case you're not aware of the draft/unpublished features in 2sxc, we want to note that each item can be live/draft, and each item could have a corresponding counterpart. So a draft-item could have a live-item (but doesn't have to), and a live-item could have a draft item.
This is important, because the admin/editor will see all the draft items, while the end-user will only see the live ones. So the exact items shown and the item-count can differ if you are logged in.
App Settings and Resources
In the App dialogs you can manage
Resources. Basically both are a content-item with the fields you specify, the only difference is the purpose they have.
- You should put button-labels, standard-texts, decorative images etc. into
Resourcesand these will often change from language to language.
- You should put settings like "what page is xxx on" or "the primary color is #53aaff" into
You would normally use it like this:
<h1 class='@App.Settings.HeadingsDecorators'> @Content.Title </h1> <div> @Content.Body </div> <a href="@App.Settings.DisclaimerPage"> @App.Resources.Disclaimer </a>
As you can see, the
DisclaimerPage are best placed in the
Settings, while the label of the
Disclaimer link are best handled as a multi-language
- If you need to get an App object for other apps, read External App Use
- If you want to use the App object from non 2sxc-code, like other MVC pages, check out External App Use
Demo App and further links
You should find some code examples in this demo App
- Blog App showing many such features
- Introduced in 2sxc 05.05
- Stable since 2sxc 06.00
- Data-API was introduced in 2sxc 06.05