Lesson 3
Adding Constraints to Route Parameters
Introduction

Welcome back! So far, you've covered the basics of routing in ASP.NET Core, including working with parameters, literal segments, and using optional and default values. In this lesson, you'll delve into applying constraints to route parameters. These constraints help ensure that only specific values pass through your routes, increasing the robustness and reliability of your applications.

Understanding Route Constraints

In large applications with numerous routes, it's common for routes to become ambiguous. For example, consider a route like /blog/{year=2024}/{slug?}. This route does not prevent ambiguities because both /blog/2023 (a valid year) and /blog/article-title (a slug) can match.

To solve this, you can define constraints for route parameters to ensure that only specific values pass through your routes. For instance, you can define a constraint to ensure year must be an integer greater than or equal to 2000: /blog/{year:int:min(2000)}/{slug?}.

With this constraint, requests like /blog/article-title will no longer be mistaken for a valid year parameter and will not match this route unless article-title is a valid year. This constraint ensures that only valid years and string slugs are processed correctly, thereby eliminating ambiguities.

Route Constraints Overview

Below you can see all route constraints available in ASP.NET Core:

ConstraintDescriptionMatching Values
{id:int}Ensures the parameter is an integer.1, 123, -456
{id:guid}Ensures the parameter is a GUID.123e4567-e89b-12d3-a456-426614174000
{id:decimal}Ensures the parameter is a decimal.1.23, 45.67, -89.01
{id:min(10)}Ensures the parameter is at least 10.10, 11, 999
{id:length(6)}Ensures the parameter is exactly 6 chars.abcdef, 123456

It's also possible to combine multiple constraints by separating them with colons, like so: id:int:min(10)

Route Constraints in Action

Let's take a look at constraints in action! In the following example, you'll define two endpoints with different constraints on the id parameter.

C#
1var builder = WebApplication.CreateBuilder(args); 2var app = builder.Build(); 3 4app.MapGet("/todo/{id:int}", (int id) => Results.Ok($"Integer ID: {id}")); 5 6app.MapGet("/todo/{id:guid}", (Guid id) => Results.Ok($"GUID ID: {id}")); 7 8app.Run();

The first endpoint will handle requests where id is an integer, such as /todo/123.

The second endpoint will handle requests where id is a valid GUID, such as /todo/123e4567-e89b-12d3-a456-426614174000.

Summary

In this lesson, you've explored the importance of route constraints in ASP.NET Core to avoid ambiguities and ensure only valid data routes through your application. By applying constraints to route parameters, you can significantly enhance the security, reliability, and overall user experience of your web applications. Next, you'll get hands-on experience with these concepts through some practical exercises. See you there!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.