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.
Selected: Typed (2sxc 16+) Switch to 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 new RazorTyped it's on MyContext.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.RazorTyped

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

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

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

⬇️ Result | Source ➡️

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

<ol>
  <li>
    <code>.CurrentCode</code> Current code culture is:
    <strong>@MyContext.Culture.CurrentCode</strong>
  </li>
  <li>
    <code>.DefaultCode</code>
    @if (Text.Has(MyContext.Culture.DefaultCode)) {
      <strong>@MyContext.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 new RazorPro it's on MyContext.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.RazorTyped
@using ToSic.Razor.Blade

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

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

On the new RazorTyped it's on MyPage or on MyContext.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 c120=page&tut=code-context
  4. .Parameters[name] Access a specific parameter (in this case the parameter c140 from the url)
@inherits Custom.Hybrid.RazorTyped
@using ToSic.Razor.Blade

<ol>
  <li>
    <code>.Id</code> of the current page: 
    <strong>@MyPage.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>@MyPage.Url</strong>
  </li>
  <li>
    <code>.Parameters</code> all the url parameters of the page 
    <code>@MyPage.Parameters</code>
  </li>
  <li>
    <code>.Parameters[name]</code> Access a specific parameter 
    (in this case the parameter <code>c140</code> from the url)
    <strong>
      @MyPage.Parameters["c140"]
    </strong>
  </li>
</ol>

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

On the new RazorTyped it's on MyContext.Module.

⬇️ Result | Source ➡️

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

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

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

On the new RazorTyped it's on MyUser or on MyContext.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.RazorTyped
@using ToSic.Razor.Blade

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

Get information about the view context.

On the new RazorTyped it's on MyView or on MyContext.View.

⬇️ Result | Source ➡️

  1. .Id of the current view: 46574
  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.RazorTyped
@using ToSic.Razor.Blade

<ol>
  <li>
    <code>.Id</code> of the current view:
    <strong>@MyView.Id</strong>
  </li>
  <li>
    <code>.Edition</code> of the current view: 
    <strong>@MyView.Edition</strong>
    (Get information about the edition used or return an empty string)
  </li>
  <li>
    <code>.Identifier</code> of the view: 
    <strong>@MyView.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>@MyView.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>