Skip to main content
Home  › ... Razor

Hybrid Razor Templates

Tutorial HomeHybrid

Platform Specific Sub-Razor-Files

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

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

Output

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

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

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 Sub-Razor-Files</h2>
      <p>
        In some cases it's just simple to provide different <code>cshtml</code> files for each platform. 
      </p>
      <p>
        Using <code>CmsContext.Platform.Name</code> you can switch between files using CmsContext.Platform.Name. Here's a simple example.
      </p>
    </div>
  </div>

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



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