Skip to content

Commit a7d32fd

Browse files
committed
Implement AddRoutingCore to support excluding Regex.
1 parent 0a3a01b commit a7d32fd

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

src/DefaultBuilder/src/WebApplicationBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilde
5959

6060
bootstrapHostBuilder.ConfigureWebHostDefaults(webHostBuilder =>
6161
{
62+
webHostBuilder.ConfigureServices(services =>
63+
{
64+
services.AddRouting();
65+
});
66+
6267
// Runs inline.
6368
webHostBuilder.Configure(ConfigureApplication);
6469

@@ -136,6 +141,11 @@ internal WebApplicationBuilder(WebApplicationOptions options, bool slim, Action<
136141
{
137142
AspNetCore.WebHost.UseKestrel(webHostBuilder);
138143

144+
webHostBuilder.ConfigureServices(services =>
145+
{
146+
services.AddRoutingCore();
147+
});
148+
139149
webHostBuilder.Configure(ConfigureEmptyApplication);
140150

141151
webHostBuilder.UseSetting(WebHostDefaults.ApplicationKey, _hostApplicationBuilder.Environment.ApplicationName ?? "");

src/DefaultBuilder/src/WebHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ internal static void UseKestrel(IWebHostBuilder builder)
257257
services.AddTransient<IStartupFilter, ForwardedHeadersStartupFilter>();
258258
services.AddTransient<IConfigureOptions<ForwardedHeadersOptions>, ForwardedHeadersOptionsSetup>();
259259

260-
services.AddRouting();
260+
services.AddRoutingCore();
261261
});
262262
}
263263

src/Http/Routing/src/DependencyInjection/RoutingServiceCollectionExtensions.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@ namespace Microsoft.Extensions.DependencyInjection;
2020
/// </summary>
2121
public static class RoutingServiceCollectionExtensions
2222
{
23+
public static IServiceCollection AddRouting(this IServiceCollection services)
24+
{
25+
return services.AddRoutingCore(routeOptions =>
26+
{
27+
routeOptions.AddRegexConstraints();
28+
});
29+
}
30+
2331
/// <summary>
2432
/// Adds services required for routing requests.
2533
/// </summary>
2634
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
2735
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
28-
public static IServiceCollection AddRouting(this IServiceCollection services)
36+
public static IServiceCollection AddRoutingCore(this IServiceCollection services)
2937
{
3038
ArgumentNullException.ThrowIfNull(services);
3139

@@ -101,21 +109,32 @@ public static IServiceCollection AddRouting(this IServiceCollection services)
101109
return services;
102110
}
103111

112+
public static IServiceCollection AddRouting(
113+
this IServiceCollection services,
114+
Action<RouteOptions> configureOptions)
115+
{
116+
return services.AddRoutingCore(routeOptions =>
117+
{
118+
routeOptions.AddRegexConstraints();
119+
configureOptions(routeOptions);
120+
});
121+
}
122+
104123
/// <summary>
105124
/// Adds services required for routing requests.
106125
/// </summary>
107126
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
108127
/// <param name="configureOptions">The routing options to configure the middleware with.</param>
109128
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
110-
public static IServiceCollection AddRouting(
129+
public static IServiceCollection AddRoutingCore(
111130
this IServiceCollection services,
112131
Action<RouteOptions> configureOptions)
113132
{
114133
ArgumentNullException.ThrowIfNull(services);
115134
ArgumentNullException.ThrowIfNull(configureOptions);
116135

117136
services.Configure(configureOptions);
118-
services.AddRouting();
137+
services.AddRoutingCore();
119138

120139
return services;
121140
}

src/Http/Routing/src/RouteOptions.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
66
using Microsoft.AspNetCore.Routing.Constraints;
7+
using Microsoft.Extensions.DependencyInjection;
78

89
namespace Microsoft.AspNetCore.Routing;
910

@@ -89,6 +90,16 @@ public IDictionary<string, Type> ConstraintMap
8990
/// </summary>
9091
internal IDictionary<string, Type> TrimmerSafeConstraintMap => _constraintTypeMap;
9192

93+
/// <summary>
94+
/// Add Regex-based constraints to the constraint map (e.g. alpha and regex constraint types). This is called automatically by <see cref="RoutingServiceCollectionExtensions.AddRouting(IServiceCollection)"/>.
95+
/// </summary>
96+
public void AddRegexConstraints()
97+
{
98+
// Regex-based constraints
99+
AddConstraint<AlphaRouteConstraint>(_constraintTypeMap, "alpha");
100+
AddConstraint<RegexInlineRouteConstraint>(_constraintTypeMap, "regex");
101+
}
102+
92103
private static IDictionary<string, Type> GetDefaultConstraintMap()
93104
{
94105
var defaults = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
@@ -113,10 +124,6 @@ private static IDictionary<string, Type> GetDefaultConstraintMap()
113124
AddConstraint<MaxRouteConstraint>(defaults, "max");
114125
AddConstraint<RangeRouteConstraint>(defaults, "range");
115126

116-
// Regex-based constraints
117-
AddConstraint<AlphaRouteConstraint>(defaults, "alpha");
118-
AddConstraint<RegexInlineRouteConstraint>(defaults, "regex");
119-
120127
AddConstraint<RequiredRouteConstraint>(defaults, "required");
121128

122129
// Files
@@ -152,7 +159,7 @@ public void SetParameterPolicy(string token, [DynamicallyAccessedMembers(Dynamic
152159
_constraintTypeMap[token] = type;
153160
}
154161

155-
private static void AddConstraint<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TConstraint>(Dictionary<string, Type> constraintMap, string text) where TConstraint : IRouteConstraint
162+
private static void AddConstraint<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TConstraint>(IDictionary<string, Type> constraintMap, string text) where TConstraint : IRouteConstraint
156163
{
157164
constraintMap[text] = typeof(TConstraint);
158165
}

0 commit comments

Comments
 (0)