In this Video, I will discuss the Web API Attribute Routing Route Constraints with examples. We are going to work with the same example that we worked in our previous Videos. So, please read the following Videos before proceeding to this Video. Attribute Routing in Web API Optional URI Parameters and Default values in Attribute Routing Attribute Routing Route Prefix in WEB API

Web API Attribute Routing Route Constraints
The Web API Attribute Routing Route Constraints are nothing but a set of rules that we can apply on our routing parameters to restrict how the parameters in the route template are matched. The general syntax is
{parameter:constraint}
Let us understand ASP.NET Web API Attribute Routing Route Constraints with one example.
Lets modify the Students Controller as shown below.
Now, if we navigate to /students/1 URI, then the GetStudentDetails(int studentID) action is executed and we get the details of the student whose id is 1 as expected.
Lets change our business requirement, in addition to retrieving the student details by “student Id”, we also want to retrieve the student details by “student Name“. So lets add another GetStudentDetails() action method with a string parameter as shown below.
At this point build the solution, and navigate to the following URIs
/students/1
/students/Pranaya
In both the cases we will get the below error:
Multiple actions were found that match the request: GetStudentDetails on type AttributeRoutingInWEBAPI.Controllers.StudentsController GetStudentDetails on type AttributeRoutingInWEBAPI.Controllers.StudentsController
This is because the WEB API Framework does not know or does not identify which version of the GetStudentDetails() action method to use. This is the situation where the route constraints play a very important role.
If an integer is specified in the URI like /students/1, then we need to execute the GetStudentDetails(int studentId) action method which takes an integer parameter whereas if a string is specified in the URI like /students/Pranaya, then we need to execute the GetStudentDetails(string studentName) action method which takes the parameter of type string.
This can be very easily achieved using Attribute Route Constraints in the WEB API application. To specify the attribute route constraint, the syntax is “{parameter:constraint}“. With these constraints in place, if the parameter segment in the URI is an integer, then the GetStudentDetails(int studentId) action method with integer parameter is invoked and if it is a string value then the GetStudentDetails(string studentName) action method with string parameter is invoked.
Lets modify the Student Controller to use the Attribute Route Constraints as shown below to achieve the above requirements.
Now build the solution, and navigate to the following two URIs and see everything is working as expected.
/students/1
/students/Pranaya
Please note that “alpha” stands for uppercase or lowercase alphabet characters. Along with alpha and int, you can also use constraints such as decimal, float, long, double, bool, etc. Please check the following MSDN link for the full list of available constraints in web API.
Example:
If you want GetStudentDetails(int studentId) action method to be mapped to URI /students/{studentId}, only if the studentId is a number greater than ZERO, then use the “min” constraint as shown below.
With the above change, if we specify a positive number like 1 in the URI, then it will be mapped to the GetStudentDetails(int studentID) action method as expected
/students/1
However, if we specify 0 or a negative number less than ZERO, then we will get an error. For example, if we specify 0 as the value for studentID in the URI,
/students/0
We will get the below error
Along with the “min” constraint, you can also specify the “max” constraint as shown below. For example, if you want the studentID value in the URI to be between 1 and 3 inclusive, then you can specify both “min” and “max” constraints as shown below.
The above example can also be achieved using the “range” attribute as shown below
Custom Web API Route Constraints in Attribute Routing
You can also create custom route constraints in Web API and to do so you need to implement the IHttpRouteConstraint interface. For example, the below constraint will restrict a parameter value to a non-zero integer value.
The following code shows how to register the custom constraint:
Now you can apply the custom constraint in your routes as shown below.