Razor is mostly HTML
+ C#
. But we need often need .net features, which can vary a bit from .net Framework and DNN, or .net core and Oqtane.
Preprocessor directives such as #if
, #if !
, #else
and #endif
can be used for simple switching of 2-3 lines of code. This is ideal if for example the Request
object has a slightly different signature or if you need to get a list of pages which is different in Dnn and Oqtane.
⬇️ Result | Source ➡️
This is DNN (not a .net core app) version: 9.11.0.46
@inherits Custom.Hybrid.Razor14
@{#if NETCOREAPP}
This is Oqtane (a .net core app) version: @Oqtane.Shared.Constants.Version
@{#else}
This is DNN (not a .net core app) version: @DotNetNuke.Application.DotNetNukeContext.Current.Application.Version
@{#endif}
Important Limitations to Preprocessors in Razor
Razor only has limited support for Preprocessor directives. For example, you cannot conditionally use @inherits
statements.
If you need this, the workaround is to create an entry-Razor which works everywhere, and two separate files which are called depending on the platform.