Constrained Route parameters
By default your route parameters are assumed to be strings.
If you know you’re expecting something else (like an int
) you can constrain your route parameter to this type.
So long as you declare a corresponding parameter in your code (with the same name and type) you’ll be guaranteed that int
.
// this constrains the parameter to an int@page "/Product/{id:int}"
// this (without the constraint) would fail// (unable to cast the value to an integer)// @page "/Product/{id}"
<h1>Product Details for @Id</h1>
@code { [Parameter] public int Id { get; set; }}