Skip to content

ConfigurableRoutesExtensions Class

Provides extension methods for enabling support for tokenized route templates like [route:MyToken?Fallback] in ASP.NET Core controllers.

C#
public static class ConfigurableRoutesExtensions

Inheritance System.Object → ConfigurableRoutesExtensions

Methods

ConfigurableRoutesExtensions.ConfigureRoutes(this IServiceCollection, IConfigurationSection, string) Method

Enables the resolution of route tokens such as [route:MyToken?Fallback] using values from the specified configuration section.

C#
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureRoutes(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, Microsoft.Extensions.Configuration.IConfigurationSection configSection, string prefix="route");

Parameters

services Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection to register services with.

configSection Microsoft.Extensions.Configuration.IConfigurationSection

The configuration section that contains token-to-value mappings.

prefix System.String

The token prefix used in route templates. Defaults to "route", which supports templates like [route:MyToken?Fallback].

Returns

Microsoft.Extensions.DependencyInjection.IServiceCollection
The same Microsoft.Extensions.DependencyInjection.IServiceCollection instance to support method chaining.

Remarks

This method allows route templates in controllers to include tokens that are dynamically resolved at application startup using values from configuration. If a token is not found, an optional fallback value can be specified using the ? delimiter.

Example:

C#
[Route("[route:ApiPrefix?v1]/users")]
public class UsersController : ControllerBase { ... }

With configuration:

C#
"Routes": {
  "ApiPrefix": "v2"
}

The resulting route will be v2/users. If "ApiPrefix" is not configured, it falls back to v1.

ConfigurableRoutesExtensions.ConfigureRoutesFallback(this IServiceCollection, string) Method

Registers a fallback-only route token resolution convention that uses only the fallback values specified in templates like [route:MyToken?Fallback], ignoring configuration.

C#
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureRoutesFallback(this Microsoft.Extensions.DependencyInjection.IServiceCollection services, string prefix="route");

Parameters

services Microsoft.Extensions.DependencyInjection.IServiceCollection

The service collection to register services with.

prefix System.String

The token prefix used in route templates. Defaults to "route", which supports templates like [route:MyToken?Fallback].

Returns

Microsoft.Extensions.DependencyInjection.IServiceCollection
The same Microsoft.Extensions.DependencyInjection.IServiceCollection instance to support method chaining.

Remarks

This method ensures that route tokens are resolved to fallback values even if the consumer of the component does not explicitly provide a configuration section.