Skip to main content
Home  › ... Razor

Hybrid Razor Templates

Tutorial HomeHybrid
Requirements
Resources

Platform Specific Razor using Preprocessor #if

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.


Output

This is DNN (not a .net core app) version: 9.11.0.46
@{#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}

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

@inherits Custom.Hybrid.Razor14
<!-- unimportant stuff, hidden -->

<div @Sys.PageParts.InfoWrapper()>
  @Html.Partial("../shared/DefaultInfoSection.cshtml")
  <div @Sys.PageParts.InfoIntro()>
    <h2>Platform Specific Razor using Preprocessor <code>#if</code></h2>
    <p>
      Razor is mostly <code>HTML</code> + <code>C#</code>. 
      But we need often need .net features, which can vary a bit from .net Framework and DNN, or .net core and Oqtane.
    </p>
    <p>
      Preprocessor directives such as <code>#if</code>, <code>#if !</code>, <code>#else</code> and <code>#endif</code> 
      can be used for simple switching of 2-3 lines of code. 
      This is ideal if for example the <code>Request</code> object has a slightly different signature
      or if you need to get a list of pages which is different in Dnn and Oqtane.
    </p>
    <div class="alert alert-warning">
      <h3>
        Important Limitations when using this
      </h3>
      <p>
        Razor only has limited support for Preprocessor directives. 
        For example, you cannot conditionally use <code>@@inherits</code> statements. 
      </p>
    </div>
  </div>
</div>

</p>

<hr>

  @{#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}


<!-- unimportant stuff, hidden -->

@* Footer *@
@Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })