But, what happens when a request comes with URL that contains three segments and third segments (id parameter) contain non integer value?
Unfortunately, you will get the following error. Here your intention is to match the URLs that contain three segments and third segment (id) must be an int.
But the value of the route parameter {id} is non integer (i.e 'mannan') and the Details() method expects an integer parameter. since value of id is not converted into an int. so the method will not execute.
In above case, you can avoid this type of error by specifying the route constraint.
Route constraint is just a condition that must be satisfied for the route to match with incoming request url.
In above case you only need the in-line integer constraint. In-line constraint is just specifying the constraint in the route template.
- public class StudentController : Controller
- {
-
- [Route("student/{id:int}")]
- public ActionResult Details(int id)
- {
- return View();
- }
- }
List of in-line constraints.
1. {param:bool} = Boolean value
2. {param:datetime} = DateTime value
3. {param:decimal} = Decimal value
4. {param:double} = Double value
5. {param:float} = Single value
6. {param:guid} = Guid value
7. {param:int} = Int32 value
8. {param:long} = Int64 value
9. {param:minlength(n)} = string value with at least N number of characters (i.e. {param:minlength(5)} )
10. {param:maxlength(n)} = string value with less than N number of characters (i.e. {param:maxlength(5)} )
11. {param:length(n)} = string value with exactly N number of characters (i.e. {param:length(3)})
12. {param:length(minNumber,maxNumber)} = string value with minimum character and maximum character length.
13. {param:min(n)} = An Int64 value that is greater than or equal to N.(i.e. {param:min(2)})
14. {param:max(n)} = An Int64 value that is less than or equal to N.(i.e. {param:min(10)})
15. {param:range(min,max)}= An Int64 value that should be within range. (i.e. {param:range(2,5)})
16. {param:alpha} =s tring value containing only the A–Z and a–z characters
17. {param:regex (pattern)} = (i.e {param:regex (\d+)}) match only number