App


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 Oqtane
@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 ➡️

.net Core code = Oqtane 💧

On Oqtane or any .net core system you should see this because CmsContext.Platform.Name was Oqtane.
Here you can also place code which only works in Oqtane, like: Version: 5.0.2

@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.

Important Limitations when using this

Razor only has limited support for Preprocessor directives. For example, you cannot conditionally use @inherits statements.

⬇️ Result | Source ➡️

This is Oqtane (a .net core app) version: 5.0.2
@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}

 

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