Skip to main content

Basic Tutorials

Tutorial Home

Working with URL Parameters using Page Parameters

The following code will use a value from the url querystring. There are various ways to pick it up, but we recommend the cross-platform API which works in Dnn and Oqtane.

  1. Cross-Platform (Dnn and Oqtane): @CmsContext.Page.Parameters["id"]
  2. This would only work in Dnn: @Request.QueryString["id"]

Usually you would have somepagename?id=27 in the url, and then using @CmsContext.Page.Parameters["id"] you would pick it up.
But with DNN, there is an additional processing that happens, as DNN tries to create nice URLs resulting in somepagename/id/27. But on the server, this is still treated as the ?id=27, so you still use the same method to access it.

Test changing this page URL

Below you'll find some links which change the url, so you can see how the output changes:


<ul>
  <li><a href='@Link.To(parameters: "?basics310=page")'>This page without additional url-parameter</a></li>
  <li><a href='@Link.To(parameters: "?basics310=page&sort=ascending")'>This page with additional <code>sort=ascending</code></a></li>
</ul>

Basics: just show a text-value of sort from the Query-String

  • URL contains sort using null-check: False
  • URL contains sort using Razor.Blade (this will also return false, if sort is empty): False
  • Sort value from URL:
  • Put sort in a variable for further use:
  • Get sort from url, or if it doesn't exist, use a default value descending (uses Razor.Blade): descending

<ul>
  <li>URL contains <code>sort</code> using null-check: 
    @(CmsContext.Page.Parameters["sort"] != null)
  </li>
  <li>URL contains <code>sort</code> using <a href="https://github.com/DNN-Connect/razor-blade" target="_blank">Razor.Blade</a> (this will also return false, if sort is empty):
    @Text.Has(CmsContext.Page.Parameters["sort"])
  </li>
  <li>Sort value from URL:
    @CmsContext.Page.Parameters["sort"]
  </li>
  <li>
    Put <code>sort</code> in a variable for further use:
    @{
      var sort = CmsContext.Page.Parameters["sort"];
    }
    @sort
  </li>
  <li>
    Get sort from url, or if it doesn't exist, use a default value <code>descending</code> (uses <a href="https://github.com/DNN-Connect/razor-blade" target="_blank">Razor.Blade</a>):
    @{
      sort = Text.First(CmsContext.Page.Parameters["sort"], "descending");
    }
    @sort
  </li>
</ul>

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
@using ToSic.Razor.Blade;
<!-- unimportant stuff, hidden -->
Working with URL Parameters using Page... <!-- unimportant stuff, hidden -->


<ul>
  <li><a href='@Link.To(parameters: "?basics310=page")'>This page without additional url-parameter</a></li>
  <li><a href='@Link.To(parameters: "?basics310=page&sort=ascending")'>This page with additional <code>sort=ascending</code></a></li>
</ul>


<hr>
<h3>Basics: just show a text-value of <code>sort</code> from the Query-String</h3>

<ul>
  <li>URL contains <code>sort</code> using null-check: 
    @(CmsContext.Page.Parameters["sort"] != null)
  </li>
  <li>URL contains <code>sort</code> using <a href="https://github.com/DNN-Connect/razor-blade" target="_blank">Razor.Blade</a> (this will also return false, if sort is empty):
    @Text.Has(CmsContext.Page.Parameters["sort"])
  </li>
  <li>Sort value from URL:
    @CmsContext.Page.Parameters["sort"]
  </li>
  <li>
    Put <code>sort</code> in a variable for further use:
    @{
      var sort = CmsContext.Page.Parameters["sort"];
    }
    @sort
  </li>
  <li>
    Get sort from url, or if it doesn't exist, use a default value <code>descending</code> (uses <a href="https://github.com/DNN-Connect/razor-blade" target="_blank">Razor.Blade</a>):
    @{
      sort = Text.First(CmsContext.Page.Parameters["sort"], "descending");
    }
    @sort
  </li>
</ul>


<!-- unimportant stuff, hidden -->