Settings / Resources Differences in APIs
2sxc has changed a lot over time. So you'll find old code snippets and new ones, and it helps to see which one is being used. Sometimes you'll also want to convert old code to new code, and this page should help you with that.
Settings and Resources Objects
Typed Code
In Typed Code, the objects you'll use are usually:
AllSettings
- a ITypedStack with all settingsAllResources
- a ITypedStack with all resourcesApp.Settings
- a ITypedItem with app settingsApp.Resources
- a ITypedItem with app resources
To get a value in typed code, you will use typed methods such as .String(...)
.
These can also have longer keys inside them, such as:
var width = AllSettings.String("Images.Content.Width");
Tip
The typed code is much more robust, as it will not throw an error if a setting doesn't exist.
So getting Images.MyConfig.Width
will not throw an error if MyConfig
doesn't exist.
Strong Typed Code
In Strong Typed Code - inheriting from AppCode.Razor.AppRazor
, the objects you'll use are usually:
AllSettings
- a ITypedStack with all settingsAllResources
- a ITypedStack with all resourcesApp.Settings
- aAppCode.Data.AppSettings
based on ITypedItem with app settingsApp.Resources
- aAppCode.Data.AppResources
based on ITypedItem with app resources
Tip
The strong-typed code is much more robust - and typed. So using App.Resources will provide intellisense (if VS Code is configured correctly).
You can also use the ?.
operator to avoid null-errors, such as App.Resources?.Title
since we're using the new Roslyn compiler for strong-typed code.
Dynamic Code
In Dynamic Code, the objects you'll use are usually:
Settings
- a dynamic object with all settingsResources
- a dynamic object with all resourcesApp.Settings
- a dynamic object with app settingsApp.Resources
- a dynamic object with app resources
To get a value in dynamic code, you will use the dynamic object directly, such as:
var width = Settings.Images.Content.Width;
Warning
The dynamic code is much more error-prone, as it will throw an error if a setting doesn't exist.
So getting Images.MyConfig.Width
will throw a null-error if MyConfig
doesn't exist.
Unfortunately you also can't use the ?.
operator to avoid this, because the old Dnn compiler doesn't support it.
Get Settings and Resources
Dynamic | Typed | Strong Typed | Comments / Differences |
---|---|---|---|
Settings ( dynamic ) |
AllSettings (ITypedStack) |
AllSettings (ITypedStack) |
All settings |
Settings.Color ( dynamic ) |
AllSettings.String("Color") ( string ) |
AllSettings.String("Color") ( string ) |
Get a color setting |
Resources ( dynamic ) |
AllResources (ITypedStack) |
AllResources (ITypedStack) |
All resources |
Resources.Title ( dynamic ) |
AllResources.String("Title") ( string ) |
AllResources.String("Title") ( string ) |
Get a resource |
App.Settings ( dynamic ) |
App.Settings (ITypedItem) |
App.Settings ( AppCode.Data.AppSettings ) |
App settings |
App.Settings.Color ( dynamic ) |
App.Settings.String("Color") ( string ) |
App.Settings.String("Color") ( string ) or App.Settings.Color ( string ) |
App settings |
App.Resources ( dynamic ) |
App.Resources (ITypedItem) |
App.Resources ( AppCode.Data.AppResources ) |
App resources |
App.Resources.Title ( dynamic ) |
App.Resources.String("Title") ( string ) |
App.Resources.String("Title") ( string ) or App.Resources.Title ( string ) |
App resources |
Tip
The new AllSettings
and AllResources
can use paths to deeper values, such as
AllSettings.Int("Images.Content.Width)
.
This is much safer that the old API, which threw an error if an intermediate setting didn't exist.