Skip to main content
Home  › ... Razor

Context Tutorials

Tutorial HomeContext
The samples can differ based on your Razor base class or if you're running an old version.
Switch to Typed (2sxc 16+) Selected: Dynamic (Razor14 or below)

Work with the Context (Page, Site, etc.)

Whenever you need to know about the current environment, you need Context information. These are the contexts you should know about:

  1. Platform
  2. Site
  3. Page
  4. Module
  5. View
  6. User

With the ICmsContext object you can get general information about the current platform.

On the older Razor14 or Razor12 it's on CmsContext.Platform.

⬇️ Result | Source ➡️

  1. .Name Current platform name is: Dnn
  2. .Type Current platform type is: Dnn
  3. .Version Current platform version is: 9.11.0.46
@inherits Custom.Hybrid.Razor14

<ol>
  <li>
    <code>.Name</code> Current platform name is: 
    <strong>@CmsContext.Platform.Name</strong>
  </li>
  <li>
    <code>.Type</code> Current platform type is: 
    <strong>@CmsContext.Platform.Type</strong>
  </li>
  <li>
    <code>.Version</code> Current platform version is: 
    <strong>@CmsContext.Platform.Version</strong>
  </li>
</ol>

With this object you can get general information about the culture.

On the new RazorTyped it's on CmsContext.Culture.

⬇️ Result | Source ➡️

  1. .CurrentCode Current code culture is: en-us
  2. .DefaultCode en-us
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade

<ol>
  <li>
    <code>.CurrentCode</code> Current code culture is:
    <strong>@CmsContext.Culture.CurrentCode</strong>
  </li>
  <li>
    <code>.DefaultCode</code>
    @if (Text.Has(CmsContext.Culture.DefaultCode)) {
      <strong>@CmsContext.Culture.DefaultCode</strong>
    } else {
      <p>
        The system is single language and the response is an empty string.
      </p>
    }
  </li>
</ol>

With this object you can get general informtion about the current site.

On the older Razor14 or Razor12 it's on CmsContext.Site.

⬇️ Result | Source ➡️

  1. .Id of the current site (same as PortalId in DNN): 24
  2. .Url The site url with protocol: https://2sxc.org/dnn-tutorials/en
  3. .UrlRoot The site url without a protocol: 2sxc.org/dnn-tutorials/en
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade

<ol>
  <li>
    <code>.Id</code> of the current site 
    (same as <code>PortalId</code> in DNN):
    <strong>@CmsContext.Site.Id</strong>
  </li>
  <li>
    <code>.Url</code> The site url with protocol:
     <strong>@CmsContext.Site.Url</strong>
  </li>
  <li>
    <code>.UrlRoot</code> The site url without a protocol: 
    <strong>@CmsContext.Site.UrlRoot</strong>
  </li>
</ol>

Get information about the page which is the contxt for the currently running code.

On the older Razor14 or Razor12 it's on CmsContext.Page.

⬇️ Result | Source ➡️

  1. .Id of the current page: 1193 (TabId in DNN or Page.PageId in oqtane.)
  2. .Url the page url with protocol: https://2sxc.org/dnn-tutorials/en/razor
  3. .Parameters all the url parameters of the page ui130=page&tut=code-context&variant=dyn
  4. .Parameters[name] Access a specific parameter (in this case the parameter c140 from the url)
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade

<ol>
  <li>
    <code>.Id</code> of the current page: <strong>@CmsContext.Page.Id</strong>
    (<code>TabId</code> in DNN or <code>Page.PageId</code> in oqtane.)
  </li>
  <li>
    <code>.Url</code> the page url with protocol: <strong>@CmsContext.Page.Url</strong>
  </li>
  <li>
    <code>.Parameters</code> all the url parameters of the page <code>@CmsContext.Page.Parameters</code>
  </li>
  <li>
    <code>.Parameters[name]</code> Access a specific parameter (in this case the parameter <code>c140</code> from the url)
    <strong>
      @CmsContext.Page.Parameters["c140"]
    </strong>
  </li>
</ol>

Get information about the module context the code is running in.

On the older Razor14 or Razor12 it's on CmsContext.Module.

⬇️ Result | Source ➡️

  1. .Id of the current module: 4480
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade

<ol>
  <li>
    <code>.Id</code> of the current module: 
    <strong>@CmsContext.Module.Id</strong>
  </li>
</ol>

Get information about the user which is currently used by the code.

On the older Razor14 or Razor12 it's on CmsContext.User.

⬇️ Result | Source ➡️

  1. .Id of the current user: -1
  2. .IsSiteAdmin False (check if the current user is a site admin)
  3. .IsContentAdmin False (check if the current user can edit content)
  4. .IsSystemAdmin False (check if the current user is a supe-user)
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade

<ol>
  <li>
    <code>.Id</code> of the current user: 
    <strong>@CmsContext.User.Id</strong>
  </li>
  <li>
    <code>.IsSiteAdmin</code> 
    <strong>@CmsContext.User.IsSiteAdmin</strong> 
    (check if the current user is a site admin) 
  </li>
  <li>
    <code>.IsContentAdmin</code> 
    <strong>@CmsContext.User.IsSiteAdmin</strong>
    (check if the current user can edit content) 
  </li>
  <li>
    <code>.IsSystemAdmin</code> 
    <strong>@CmsContext.User.IsSystemAdmin</strong> 
    (check if the current user is a supe-user) 
  </li>
</ol>

Get information about the view context.

On the older Razor14 or Razor12 it's on CmsContext.View.

⬇️ Result | Source ➡️

  1. .Id of the current view: 48259
  2. .Edition of the current view: (Get information about the edition used or return an empty string)
  3. .Identifier of the view:
    An optional identifier which the View configuration can provide. Use this when you want to use the same template but make minor changes based on the View selected (like change the number of columns). Usually you will use either this OR the Settings:
  4. .Name of the view Default Tutorial Page (new v16)
    Name of the view as configured - note that because of i18n it could be different depending on the language. To clearly identify a view, use the Identifier or use Settings:
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade

<ol>
  <li>
    <code>.Id</code> of the current view: 
    <strong>@CmsContext.View.Id</strong>
  </li>
  <li>
    <code>.Edition</code> of the current view: 
    <strong>@CmsContext.View.Edition</strong>
    (Get information about the edition used or return an empty string)
  </li>
  <li>
    <code>.Identifier</code> of the view: 
    <strong>@CmsContext.View.Identifier</strong>
    <br>
    An optional identifier which the View configuration can provide. 
    Use this when you want to use the same template but make minor
    changes based on the View selected (like change the number of columns). 
    Usually you will use either this OR the Settings: 
  </li>
  <li>
    <code>.Name</code> of the view <code>@CmsContext.View.Name</code>
    <br>
    Name of the view as configured - note that because of i18n 
    it could be different depending on the language. 
    To clearly identify a view, use the Identifier or use Settings: 
  </li>
</ol>