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.
Click on the links to change the url-parameters of this page and see the results below.
Output
-
Raw id from URL:
Equal to string "27": False
Equal to number 27: (would throw error)
-
Number id from URL: 0
Equal to string "27": (would throw error)
Equal to number 27: False
-
Number id from URL or -1: -1
Equal to string "27": (would throw error)
Equal to number 27: False
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 <em>number</em> URL Parameters</h2> <p> When you expect a number, you usually need to convert it to an <code>int</code> or similar for further use. Otherwise you'll be comparing numbers with strings. For example <code>if (3 == "3")</code> would return <code>false</code> - which is not what you usually want. <br> Additionally, you usually want to ensure that if no parameter is found, you will have a default value - othertise you may run into errors. </p> <p> Click on the links to change the url-parameters of this page and see the results below. </p> <ul> @SubpageLink("This page without additional params", "") @SubpageLink("put ?id=27 in the url", "id=27") </ul> </div> </div> @{ // This variable is a string, but could be null or empty var idAsString = MyPage.Parameters["id"]; // This converts the id parameter to int. First sample auto-falls back to 0, other to -1 var id = MyPage.Parameters.Int("id"); var idOrMinus1 = MyPage.Parameters.Int("id", fallback: -1); } <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> <li> <strong>Number id from URL or -1:</strong> @idOrMinus1 <br> Equal to string "27": (would throw error) <br> Equal to number 27: @(idOrMinus1 == 27) <br> </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 @{ // This variable is a string, but could be null or empty var idAsString = CmsContext.Page.Parameters["id"]; // This converts the id parameter to int. First sample auto-falls back to 0, other to -1 var id = Kit.Convert.ToInt(CmsContext.Page.Parameters["id"]); var idOrMinus1 = Kit.Convert.ToInt(CmsContext.Page.Parameters["id"], -1); } <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> <li> <strong>Number id from URL or -1:</strong> @idOrMinus1 <br> Equal to string "27": (would throw error) <br> Equal to number 27: @(idOrMinus1 == 27) <br> </li> </ul> *@ <hide> @functions { dynamic SubpageLink(string label, string urlparams) { return Tag.Li() .Wrap( Tag.A(label).Href(Link.To(parameters: "?basics311=page&" + urlparams)) ); } } @* Footer *@ @Html.Partial("../Shared/Layout/FooterWithSource.cshtml", new { Sys = Sys })