Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Route constraints in asp.net core

Suggested Videos
Part 8 - Routing in asp.net core razor pages | Text | Slides
Part 9 - Query string parameters in asp.net core razor pages | Text | Slides
Part 10 - Route parameters in asp.net core razor pages | Text | Slides

In this video we will discuss route constraints in asp.net core. The concepts we discuss here can be used in ASP.NET Core MVC web application, ASP.NET Core Web API and ASP.NET Core razor pages web application.


What is the use of route constraints

Route Constraints are a mechanism to filter out or restrict unwanted route values from reaching the controller action or the Page handler methods in a razor pages application.


The following razor page expects a value for the id route parameter. You can pass any value - numbers, alphabets, combination of both. Any value really.

@page "{id}"

Route parameter values can be constrained by data type and range. To specify a route constraint, include a colon and the constraint. In the following example, id route parameter value must be an integer. If you pass alphabets instead of integer values, you get a 404 error.

@page "{id:int}"

Route constraint parameters

Some constraints accept parameters as well. min constraint can be used to specify a minimum integer value. In our case, an employee ID must be equal to or greater than 1. 

@page "{id:min(1)}"

Multiple route constraints

Multiple Constraints can be combined using a colon. The following example ensures id value is an integer between 1 and 5.

@page "{id:min(1):max(5)}"

The same can be achieved using the range constraint as well. The range constraint takes 2 parameters. The minimum and maximum allowed value.

@page "{id:range(1,5)}"

Optional route parameters and route constraints

In the following example, the route parameter id is optional. To make it optional we have included a question mark at the end of the route parameter.

@page "{id?}"

To make a route parameter optional, when you have constraints specified, you have to include the question mark at the end of all the constraints.

@page "{id:range(1,5)?}"

If you include it immediately after the name of the route parameter, you get RoutePatternException.

@page "{id?:range(1,5)}"

RoutePatternException : The route parameter name 'id?' is invalid. Route parameter names must be non-empty and cannot contain these characters: '{', '}', '/'. The '?' character marks a parameter as optional, and can occur only at the end of the parameter. The '*' character marks a parameter as catch-all, and can occur only at the start of the parameter.

Common built-in route constraints

Constraint Example Use
int {id:int} Only integer values allowed
Similarly other built-in datatypes like decimal, float, double, datetime, bool, long, guid can also be used as constraints
alpha {firstName:alpha} String must contain only alphabets
minlength(value) {firstName:minlength(5)} String must be at least 5 characters
maxlength(value) {firstName:maxlength(10)} String must not be more than 10 characters
length(min,max) {firstName:length(5,10)} String must be at least 5 and not more than 10 characters in length
length(length) {firstName:length(10)} String must be exactly 10 characters in length
min(value) {id:min(1)} Integer value must be at least 1
max(value) {id:max(100)} Integer value must not be more than 100
range(min,max) {id:range(1,100)} Integer value must be between 1 and 100 (inclusive)
regex(expression) String must match the pattern specified by the regular expression

If none of these built-in constraints meet your requirement, you can create a custom constraint. We will discuss custom constraints in our upcoming videos.

asp.net core razor pages tutorial

No comments:

Post a Comment

It would be great if you can help share these free resources