Like everywhere else in C#, strict type constraints help us avoid errors. Imagine if someone sent a
POST request to this URL:
It would break our application, which expects integers for each of those route values. Within our
@page directive we can specify that constraint like the below, where
int stands for “integer” (we’ll show an abbreviated version here):
The general format is:
If you want the route value to remain optional, use the question mark after the constraint, like:
There are a lot of constraints out there, but here are a few to get started:
int— value must be any integer
alpha— value must consist of one or more alphabetical characters (a-z, case-insensitive)
bool— value must be true or false (case-insensitive)
A longer (but not exhaustive) list of constraints is available in the Microsoft documentation.
@page directive specifies a route value without constraints. For example, what happens when you navigate to the below “illegal” URL?
Add the proper constraint so that it only accepts integers (and is still optional).