Skip to content

Commit 2f125f1

Browse files
links to custom route constraints (#18141)
* links to custom route constraints * links to custom route constraints
1 parent 5c5d924 commit 2f125f1

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

aspnetcore/fundamentals/routing.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ Custom route constraints can be created by implementing the <xref:Microsoft.AspN
571571

572572
Custom route constraints are rarely needed. Before implementing a custom route constraint, consider alternatives, such as model binding.
573573

574+
The ASP.NET Core [Constraints](https://github.com/dotnet/aspnetcore/tree/master/src/Http/Routing/src/Constraints) folder provides good examples of creating a constraints. For example, [GuidRouteConstraint](https://github.com/dotnet/aspnetcore/blob/master/src/Http/Routing/src/Constraints/GuidRouteConstraint.cs#L18).
575+
574576
To use a custom `IRouteConstraint`, the route constraint type must be registered with the app's <xref:Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap> in the service container. A `ConstraintMap` is a dictionary that maps route constraint keys to `IRouteConstraint` implementations that validate those constraints. An app's `ConstraintMap` can be updated in `Startup.ConfigureServices` either as part of a [services.AddRouting](xref:Microsoft.Extensions.DependencyInjection.RoutingServiceCollectionExtensions.AddRouting*) call or by configuring <xref:Microsoft.AspNetCore.Routing.RouteOptions> directly with `services.Configure<RouteOptions>`. For example:
575577

576578
[!code-csharp[](routing/samples/3.x/RoutingSample/StartupConstraint.cs?name=snippet)]

0 commit comments

Comments
 (0)