When we define a route template or pattern, we may also want to specify how that route shall be matched, which is constraining it. We can constrain a route in a number of ways, such as these:
- The request needs to match a given HTTP method.
- The request needs to match a given content type.
- Its parameters need to match certain rules.
A constraint can be expressed in the route template or as a discrete object, using the MapControllerRoute method. If you choose to use the route template, you need to specify its name next to the token to which it applies:
{controller=Home}/{action=Index}/{id:int}
Notice {id:int}: this constrains the id parameter to an integer, and is one of the provided constraints that we will talk about in a moment. Another option is to make use of the defaults parameter:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default...