Skip to main content
Home  › ... Razor

Hybrid Tutorials

Tutorial HomeHybrid
#1 Hybrid Razor - Code which Runs on Dnn and Oqtane
The samples can differ based on your Razor base class or if you're running an old version.
Selected: Typed (2sxc 16+) Switch to Dynamic (Razor14 or below)

Hybrid Razor - Code which Runs on Dnn and Oqtane

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. This is where CmsContext.Platform.Name is used. Read more about this in the docs.

⬇️ Result | Source ➡️

You are running Dnn
@inherits Custom.Hybrid.Razor14

You are running
<code>@CmsContext.Platform.Name</code>

In some cases it's just simple to provide different cshtml files for each platform.

Using MyContext.Platform.Name you can switch between files using CmsContext.Platform.Name. Here's a simple example.

⬇️ Result | Source ➡️

NON .net Core code = Dnn ☢

On Dnn you'll see this because CmsContext.Platform.Name was Dnn.
Here you can also place code which only compiles in DNN, like: PortalId = 24

@inherits Custom.Hybrid.Razor14

@Html.Partial("CodeSwitching." + CmsContext.Platform.Name + ".cshtml")

Source Code of CodeSwitching.Dnn.cshtml

@inherits Custom.Hybrid.Razor14
<h3>NON .net Core code = Dnn ☢</h3>
<p>
  On Dnn you'll see this because <code>CmsContext.Platform.Name</code> was <code>Dnn</code>. <br>

  Here you can also place code which only compiles in DNN, like: 
  PortalId = @DotNetNuke.Entities.Portals.PortalSettings.Current.PortalId
</p>

Source Code of CodeSwitching.Oqtane.cshtml

@inherits Custom.Hybrid.Razor14
<h3>.net Core code = Oqtane 💧</h3>
<p>
  On Oqtane or any .net core system you should see this because <code>CmsContext.Platform.Name</code> was <code>Oqtane</code>. <br>

  Here you can also place code which only works in Oqtane, like: 
  Version: @Oqtane.Shared.Constants.Version
</p>

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. 

 

#1 Hybrid Razor - Code which Runs on Dnn and Oqtane