Route Constraints
Route Constraints enable you to constrain the type of values that you pass to the controller action. It is all about action selection for routing. So, we can say one action should trigger for integer input whereas another should trigger for non-integers. For example, if you want to restrict the value to be passed to the int
type, you can do so. The following is one such instance:
[HttpGet("details2/{id:int}")] public IActionResult Details2(int id = 123) { return View(); }
ASP.NET Core even supports default parameter values so that you can pass the default parameters:
[HttpGet("details2/{id:int}")] public IActionResult Details2(int id = 123) { return View(); }
There are many constraints available, such as int
and bool
.
Activity: Creating an Attribute that Implements IActionConstraintFactory
Scenario
By using IActionConstraint
and IActionConstraintFactory
interfaces we can add custom constraints. Create an attribute that implements IActionConstraintFactory
and...