In this recipe, we will learn how to create a route constraint using convention routing.
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
- 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" } ); ...