Logo
Logo
  • Blazor CMS Home
  • Oqtane Tutorials
  • Blazor CMS Home
  • Oqtane Tutorials
Oqtane Tutorials
Oqtane Tutorials

Basics: Linking and URL Parameters

Tutorial Home › Linking
Use Shared C# Code
#1 Working with Page URL Parameters
Linking with Number Url Parameters
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)
Requirements
  • 2sxc 12.08
Resources
  • Page Parameters
  • Link.To(...)

Working with Page URL 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. Pro mode Cross-Platform (Dnn and Oqtane): @MyPage.Parameters["id"]
  2. Older mode Cross-Platform (Dnn and Oqtane): @CmsContext.Page.Parameters["id"]
  3. This would only work in Dnn: @Request.QueryString["id"]

Usually you would have somepagename?id=27 in the url, and then using @MyPage.Parameters["id"] / @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.

Note that this sample uses Link.To(parameters: object.Add/Remove) to just modify the existing url, since this tutorial uses other url-parameters to pick the snippets to show. 

⬇️ Result | Source ➡️

FYI: The current url parameters are
tut=basics-linking&variant=dyn

Links to test the functionality

  • This page without sort url-parameter
  • This page with additional sort=ascending

Reading the url parameters

  • URL contains sort w/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
@inherits Custom.Hybrid.Razor14
@using ToSic.Razor.Blade

<p>
  FYI: The current url parameters are <br>
  <code>@CmsContext.Page.Parameters</code>
</p>

<p>Links to test the functionality</p>
<ul>
  <li><a href='@Link.To(parameters: CmsContext.Page.Parameters.Remove("sort"))'>This page without <code>sort</code> url-parameter</a></li>
  <li><a href='@Link.To(parameters: CmsContext.Page.Parameters.Set("sort", "Ascending"))'>This page with additional <code>sort=ascending</code></a></li>
</ul>


<p>Reading the url parameters</p>
<ul>
  <li>URL contains <code>sort</code> w/null-check: <br>
    @(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): <br>
    @Text.Has(CmsContext.Page.Parameters["sort"])
  </li>
  <li>Sort value from URL: <br>
    @CmsContext.Page.Parameters["sort"]
  </li>
  <li>
    Put <code>sort</code> in a variable for further use: <br>
    @{
      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>

 

Use Shared C# Code
#1 Working with Page URL Parameters
Linking with Number Url Parameters
  • 2sic internet solutions
  • Langäulistrasse 62, 9470 Buchs SG, Switzerland
  • +41 81 750 67 77
Login