Skip to content

Instantly share code, notes, and snippets.

@MoienTajik
Last active May 31, 2019 15:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoienTajik/5c5962dba7fb2de278c7eece944f3d85 to your computer and use it in GitHub Desktop.
Save MoienTajik/5c5962dba7fb2de278c7eece944f3d85 to your computer and use it in GitHub Desktop.
ASP.NET Core default Route Constraints list

ASP.NET Core default Route Constraints list

Constraints for Checking Data Type

The following Constrains checks the data type :

Constraint Inline Class Description
int {id:int} IntRouteConstraint Constrains a route parameter to represent only 32-bit integer values
alpha {id:alpha} AlphaRouteConstraint Constrains a route parameter to contain only lowercase or uppercase letters A through Z in the English alphabet.
bool {id:bool} BoolRouteConstraint Constrains a route parameter to represent only Boolean values.
datetime {id:datetime} DateTimeRouteConstraint Constrains a route parameter to represent only DateTime values.
decimal {id:decimal} DecimalRouteConstraint Constrains a route parameter to represent only decimal values.
double {id:double} DoubleRouteConstraint Constrains a route parameter to represent only 64-bit floating-point values.
float {id:float} FloatRouteConstraint Matches a valid float value (in the invariant culture - see warning)
guid {id:guid} GuidRouteConstraint Matches a valid Guid value

Constraints for Checking Data Value/Range/Length

The following Constrains checks the Value/Range/Length etc :

Constraint Inline Class Description
length(length) {id:length(12)} LengthRouteConstraint Constrains a route parameter to be a string of a given length or within a given range of lengths.
maxlength(value) {id:maxlength(8)} MaxLengthRouteConstraint Constrains a route parameter to be a string with a maximum length.
minlength(value) {id:minlength(4)} MinLengthRouteConstraint Constrains a route parameter to be a string with a maximum length.
range(min,max) {id:range(18,120)} RangeRouteConstraint Constraints a route parameter to be an integer within a given range of values.
min(value) {id:min(18)} MinRouteConstraint Constrains a route parameter to be a long with a minimum value.
max(value) {id:max(120)} MaxRouteConstraint Constrains a route parameter to be an integer with a maximum value.

Constraints using a Regular Expression

Using Regular expression or regex as constraint offers more flexibility to limit any input :

Constraint Inline Class Description
regex(expression) {ssn:regex(^\\d{{3}}-\\d{{2}}-\\d{{4}}$)} RegexRouteConstraint Constrains a route parameter to match a regular expression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment