Creating a route constraint
In this recipe, we will learn how to create a route constraint using convention routing.
Getting ready
A route constraint is inserted with an inline syntax, called inline constraints, in the following form: {parameter:constraint}.
There are several ways to create a route constraint:
- Inserting a constraint when creating a route conventionally
- Inserting a constraint when creating an attribute routing
- Creating a class that implements
IRouteConstraint
How to do it...
- Here is the first way, inserting a constraint when creating a route conventionally:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"
);
routes.MapRoute(
name: "products",
template: "Products/{id=1}",
defaults: new { controller = "Product", action = "Details" }
); ...