Skip to main content

Basic Tutorials

Tutorial Home

Working with number URL Parameters

When you expect a number, you usually need to convert it to an int or similar for further use. Otherwise you'll be comparing numbers with strings. For example if(3 == "3") would return false - which is not what you usually want.
Additionally, you usually want to ensure that if no parameter is found, you will have a default value - othertise you may run into errors.


  • Raw id from URL: 27
    Equal to string "27": True
    Equal to number 27: (would throw error)
  • Number id from URL: 27
    Equal to string "27": (would throw error)
    Equal to number 27: True

@{
  // This variable is a string, but could be null or empty
  var idAsString = CmsContext.Page.Parameters["id"];
  // This converts the id parameter to int and takes 0 as fallback
  var id = Kit.Convert.ToInt(CmsContext.Page.Parameters["id"], 0);
}
<ul>
  <li>
    <strong>Raw id from URL:</strong> @idAsString <br>
    Equal to string "27": @(idAsString == "27") <br>
    Equal to number 27: (would throw error) <br>
  </li>
  <li>
    <strong>Number id from URL:</strong> @id <br>
    Equal to string "27": (would throw error) <br>
    Equal to number 27: @(id == 27) <br>
  </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
<!-- unimportant stuff, hidden -->
@using ToSic.Razor.Blade;
Working with number URL Parameters When... <!-- unimportant stuff, hidden -->


@{
  // This variable is a string, but could be null or empty
  var idAsString = CmsContext.Page.Parameters["id"];
  // This converts the id parameter to int and takes 0 as fallback
  var id = Kit.Convert.ToInt(CmsContext.Page.Parameters["id"], 0);
}
<ul>
  <li>
    <strong>Raw id from URL:</strong> @idAsString <br>
    Equal to string "27": @(idAsString == "27") <br>
    Equal to number 27: (would throw error) <br>
  </li>
  <li>
    <strong>Number id from URL:</strong> @id <br>
    Equal to string "27": (would throw error) <br>
    Equal to number 27: @(id == 27) <br>
  </li>
</ul>



<!-- unimportant stuff, hidden -->