Skip to content

Commit 2eb28ef

Browse files
alsi-lawrguardrex
andauthored
Add exhaustive list of default route constraints (#35838)
* Add exhaustive list of default route constraints * improve readability of route constraint notes for file and nonfile Co-authored-by: Luke Latham <[email protected]> * Update aspnetcore/fundamentals/routing.md --------- Co-authored-by: Luke Latham <[email protected]>
1 parent b4b3a03 commit 2eb28ef

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

aspnetcore/fundamentals/routing.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -499,28 +499,30 @@ Route constraints execute when a match has occurred to the incoming URL and the
499499
> [!WARNING]
500500
> Don't use constraints for input validation. If constraints are used for input validation, invalid input results in a `404` Not Found response. Invalid input should produce a `400` Bad Request with an appropriate error message. Route constraints are used to disambiguate similar routes, not to validate the inputs for a particular route.
501501
502-
The following table demonstrates example route constraints and their expected behavior:
503-
504-
| constraint | Example | Example Matches | Notes |
505-
|---------------------|---------------------------------------------|----------------------------------------|-------------------------------------------------------------------------------------------|
506-
| `int` | `{id:int}` | `123456789`, `-123456789` | Matches any integer |
507-
| `bool` | `{active:bool}` | `true`, `FALSE` | Matches `true` or `false`. Case-insensitive |
508-
| `datetime` | `{dob:datetime}` | `2016-12-31`, `2016-12-31 7:32pm` | Matches a valid `DateTime` value in the invariant culture. See preceding warning. |
509-
| `decimal` | `{price:decimal}` | `49.99`, `-1,000.01` | Matches a valid `decimal` value in the invariant culture. See preceding warning. |
510-
| `double` | `{weight:double}` | `1.234`, `-1,001.01e8` | Matches a valid `double` value in the invariant culture. See preceding warning. |
511-
| `float` | `{weight:float}` | `1.234`, `-1,001.01e8` | Matches a valid `float` value in the invariant culture. See preceding warning. |
512-
| `guid` | `{id:guid}` | `CD2C1638-1638-72D5-1638-DEADBEEF1638` | Matches a valid `Guid` value |
513-
| `long` | `{ticks:long}` | `123456789`, `-123456789` | Matches a valid `long` value |
514-
| `minlength(value)` | `{username:minlength(4)}` | `Rick` | String must be at least 4 characters |
515-
| `maxlength(value)` | `{filename:maxlength(8)}` | `MyFile` | String must be no more than 8 characters |
516-
| `length(length)` | `{filename:length(12)}` | `somefile.txt` | String must be exactly 12 characters long |
517-
| `length(min,max)` | `{filename:length(8,16)}` | `somefile.txt` | String must be at least 8 and no more than 16 characters long |
518-
| `min(value)` | `{age:min(18)}` | `19` | Integer value must be at least 18 |
519-
| `max(value)` | `{age:max(120)}` | `91` | Integer value must be no more than 120 |
520-
| `range(min,max)` | `{age:range(18,120)}` | `91` | Integer value must be at least 18 but no more than 120 |
521-
| `alpha` | `{name:alpha}` | `Rick` | String must consist of one or more alphabetical characters, `a`-`z` and case-insensitive. |
522-
| `regex(expression)` | `{ssn:regex(^\\d{{3}}-\\d{{2}}-\\d{{4}}$)}` | `123-45-6789` | String must match the regular expression. See tips about defining a regular expression. |
523-
| `required` | `{name:required}` | `Rick` | Used to enforce that a non-parameter value is present during URL generation |
502+
The following table demonstrates the default route constraints and their expected behavior:
503+
504+
| constraint | Example | Example Matches | Notes |
505+
|---------------------|---------------------------------------------|----------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
506+
| `int` | `{id:int}` | `123456789`, `-123456789` | Matches any integer |
507+
| `bool` | `{active:bool}` | `true`, `FALSE` | Matches `true` or `false`. Case-insensitive |
508+
| `datetime` | `{dob:datetime}` | `2016-12-31`, `2016-12-31 7:32pm` | Matches a valid `DateTime` value in the invariant culture. See preceding warning |
509+
| `decimal` | `{price:decimal}` | `49.99`, `-1,000.01` | Matches a valid `decimal` value in the invariant culture. See preceding warning |
510+
| `double` | `{weight:double}` | `1.234`, `-1,001.01e8` | Matches a valid `double` value in the invariant culture. See preceding warning |
511+
| `float` | `{weight:float}` | `1.234`, `-1,001.01e8` | Matches a valid `float` value in the invariant culture. See preceding warning |
512+
| `guid` | `{id:guid}` | `CD2C1638-1638-72D5-1638-DEADBEEF1638` | Matches a valid `Guid` value |
513+
| `long` | `{ticks:long}` | `123456789`, `-123456789` | Matches a valid `long` value |
514+
| `minlength(value)` | `{username:minlength(4)}` | `Rick` | String must be at least 4 characters |
515+
| `maxlength(value)` | `{filename:maxlength(8)}` | `MyFile` | String must be no more than 8 characters |
516+
| `length(length)` | `{filename:length(12)}` | `somefile.txt` | String must be exactly 12 characters long |
517+
| `length(min,max)` | `{filename:length(8,16)}` | `somefile.txt` | String must be at least 8 and no more than 16 characters long |
518+
| `min(value)` | `{age:min(18)}` | `19` | Integer value must be at least 18 |
519+
| `max(value)` | `{age:max(120)}` | `91` | Integer value must be no more than 120 |
520+
| `range(min,max)` | `{age:range(18,120)}` | `91` | Integer value must be at least 18 but no more than 120 |
521+
| `alpha` | `{name:alpha}` | `Rick` | String must consist of one or more alphabetical characters, `a`-`z` and case-insensitive |
522+
| `regex(expression)` | `{ssn:regex(^\\d{{3}}-\\d{{2}}-\\d{{4}}$)}` | `123-45-6789` | String must match the regular expression. See tips about defining a regular expression |
523+
| `required` | `{name:required}` | `Rick` | Used to enforce that a non-parameter value is present during URL generation |
524+
| `file` | `{filename:file}` | `myfile.txt` | String can contain path segments, but its last segment must have a dot (`.`) and be followed by one or more non-dot characters |
525+
| `nonfile` | `{page:nonfile}` | `PageName` | String must not have a dot in its last path segment that is followed by one or more non-dot (`.`) characters |
524526

525527
[!INCLUDE[](~/includes/regex.md)]
526528

0 commit comments

Comments
 (0)