Skip to main content
Home  › ... Razor

Basics: Linking and URL Parameters

Tutorial HomeLinking
#3 Linking with Modified Page Parameters
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)

Preserve URL Parameters and More

In Razor page URLs are generated using Link.To(...). By default calling Link.To() without any parameters returns the base URL of the site. In this case it would be: https://app-dev.2sxc.org/tutorial-razor/.

The key to retrieving the current page URL is to pass the current page parameters. Link.To(parameters: ...) will then construct the URL based on the passed parameters.
Current page parameters are found in CmsContext.Page.Parameters.

@inherits Custom.Hybrid.RazorTyped
@using ToSic.Razor.Blade

@{ var currentPageUrl = Link.To(parameters: MyPage.Parameters); }
<ol>
  <li>
    <a href='@Link.To()'>Link to current page <em>without</em>
    Parameters: @Link.To()</a>
  </li>
  <li>
    <a href='@currentPageUrl'>Link to current page preserving 
    parameters @currentPageUrl</a>
  </li>
</ol>

Because MyPage.Parameters / CmsContext.Page.Parameters follows the query string convention adding a new parameter isn't too difficult.
To add a new parameter you can use the .Add(key, value) method or add the parameters as string following the convention as for example &name=2sxc.

⬇️ Result | Source ➡️

See adjusted page parameters: turn-on202=page&tut=code-link-parameters-modify&name=2sxc

Current page URL with new parameter from string: https://2sxc.org/dnn-tutorials/en/razor/turn-on202/page/tut/code-link-parameters-modify/name/2sxc

Current page URL with new parameter from .Add(...): https://2sxc.org/dnn-tutorials/en/razor/turn-on202/page/tut/code-link-parameters-modify/name/2sxc

@inherits Custom.Hybrid.RazorTyped
@using ToSic.Razor.Blade

@{
  // Example using string
  var newParamsFromString = MyPage.Parameters + "&name=2sxc";

  // Page parameters using .Add(...) method
  var newParamsFromAdd = MyPage.Parameters.Add("name", "2sxc");
}
<!-- unimportant stuff, hidden -->
<p>Current page URL with new parameter from string: @Link.To(parameters: newParamsFromString)</p>
<p>Current page URL with new parameter from <code>.Add(...)</code>: @Link.To(parameters: newParamsFromAdd)</p>

To remove a parameter you can use the .Remove(key) method or modify the string containing parameters.

⬇️ Result | Source ➡️

See adjusted page parameters: turn-on202=page&tut=code-link-parameters-modify

Current page URL with removed parameter: https://2sxc.org/dnn-tutorials/en/razor/turn-on202/page/tut/code-link-parameters-modify

@inherits Custom.Hybrid.RazorTyped
@using ToSic.Razor.Blade

@{
  var currentParamsRemoved = MyPage.Parameters.Remove("basics320");
}
<!-- unimportant stuff, hidden -->
<p>Current page URL with removed parameter: @Link.To(parameters: currentParamsRemoved)</p>

To change a parameter you can use the .Set(key, value) method or modify the string containing parameters.

⬇️ Result | Source ➡️

See adjusted page parameters: turn-on202=page&tut=code-link-parameters-modify&basics320=2sxc

Current page URL with changed parameter: https://2sxc.org/dnn-tutorials/en/razor/turn-on202/page/tut/code-link-parameters-modify/basics320/2sxc

@inherits Custom.Hybrid.RazorTyped
@using ToSic.Razor.Blade

@{
  var currentParamsChanged = MyPage.Parameters.Set("basics320", "2sxc");
}
<!-- unimportant stuff, hidden -->
<p>Current page URL with changed parameter: @Link.To(parameters: currentParamsChanged)</p>

 

 

#3 Linking with Modified Page Parameters