Requirements
Resources
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.
-
Pro mode Cross-Platform (Dnn and Oqtane):
@MyPage.Parameters["id"]
-
Older mode Cross-Platform (Dnn and Oqtane):
@CmsContext.Page.Parameters["id"]
-
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.
Test changing this page URL
Below you'll find some links which change the url, so you can see how the output changes:
Basics: just show a text-value of sort
from the Query-String
Output
- 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
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.RazorTyped @using ToSic.Razor.Blade; <!-- unimportant stuff, hidden --> <div @Sys.PageParts.InfoWrapper()> @Html.Partial("../shared/DefaultInfoSection.cshtml") <div @Sys.PageParts.InfoIntro()> <h2>Working with URL Parameters using Page Parameters</h2> <p> 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. </p> <ol> <li> Pro mode Cross-Platform (Dnn and Oqtane): <code>@@MyPage.Parameters["id"]</code> </li> <li> Older mode Cross-Platform (Dnn and Oqtane): <code>@@CmsContext.Page.Parameters["id"]</code> </li> <li> This would <em>only</em> work in Dnn: <code>@@Request.QueryString["id"]</code> </li> </ol> <p> Usually you would have <code>somepagename?id=27</code> in the url, and then using <code>@@MyPage.Parameters["id"]</code> / <code>@@CmsContext.Page.Parameters["id"]</code> you would pick it up. <br> But with DNN, there is an additional processing that happens, as DNN tries to create nice URLs resulting in <code>somepagename/id/27</code>. But on the server, this is still treated as the <code>?id=27</code>, so you still use the same method to access it. </p> <h2>Test changing this page URL</h2> <p> Below you'll find some links which change the url, so you can see how the output changes: </p> </div> </div> <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> <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: @(MyPage.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(MyPage.Parameters["sort"]) </li> <li>Sort value from URL: @MyPage.Parameters["sort"] </li> <li> Put <code>sort</code> in a variable for further use: @{ var sort = MyPage.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(MyPage.Parameters["sort"], "descending"); } @sort </li> </ul> @* Snippet for the old v14 code, which shouldn't run but should be shown. This is a bit of a hack, but it works <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> *@ @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })