diff --git a/src/Http/Routing/perf/Microbenchmarks/LinkGeneration/SingleRouteWithConstraintsBenchmark.cs b/src/Http/Routing/perf/Microbenchmarks/LinkGeneration/SingleRouteWithConstraintsBenchmark.cs
index 925bc5159db7..ea22d5cb45c2 100644
--- a/src/Http/Routing/perf/Microbenchmarks/LinkGeneration/SingleRouteWithConstraintsBenchmark.cs
+++ b/src/Http/Routing/perf/Microbenchmarks/LinkGeneration/SingleRouteWithConstraintsBenchmark.cs
@@ -1,4 +1,4 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using BenchmarkDotNet.Attributes;
@@ -17,7 +17,7 @@ public class SingleRouteWithConstraintsBenchmark : EndpointRoutingBenchmarkBase
[GlobalSetup]
public void Setup()
{
- var template = "Customers/Details/{category}/{region}/{id:int}";
+ var template = "Customers/Details/{category:alpha}/{region:alpha}/{id:int}";
var defaults = new { controller = "Customers", action = "Details" };
var requiredValues = new { controller = "Customers", action = "Details" };
diff --git a/src/Http/Routing/src/Constraints/AlphaRouteConstraint.cs b/src/Http/Routing/src/Constraints/AlphaRouteConstraint.cs
index abb503139640..687481275388 100644
--- a/src/Http/Routing/src/Constraints/AlphaRouteConstraint.cs
+++ b/src/Http/Routing/src/Constraints/AlphaRouteConstraint.cs
@@ -1,17 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Text.RegularExpressions;
+
namespace Microsoft.AspNetCore.Routing.Constraints;
///
/// Constrains a route parameter to contain only lowercase or uppercase letters A through Z in the English alphabet.
///
-public class AlphaRouteConstraint : RegexRouteConstraint
+public partial class AlphaRouteConstraint : RegexRouteConstraint
{
///
/// Initializes a new instance of the class.
///
- public AlphaRouteConstraint() : base(@"^[a-z]*$")
+ public AlphaRouteConstraint() : base(GetAlphaRouteRegex())
{
}
+
+ [GeneratedRegex(@"^[A-Za-z]*$")]
+ private static partial Regex GetAlphaRouteRegex();
}
diff --git a/src/Http/Routing/src/Constraints/RegexRouteConstraint.cs b/src/Http/Routing/src/Constraints/RegexRouteConstraint.cs
index 27b0a08a9de9..1d7d7e5e6b52 100644
--- a/src/Http/Routing/src/Constraints/RegexRouteConstraint.cs
+++ b/src/Http/Routing/src/Constraints/RegexRouteConstraint.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
@@ -33,7 +34,9 @@ public RegexRouteConstraint(Regex regex)
/// Constructor for a given a .
///
/// A string containing the regex pattern.
- public RegexRouteConstraint(string regexPattern)
+ public RegexRouteConstraint(
+ [StringSyntax(StringSyntaxAttribute.Regex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)]
+ string regexPattern)
{
if (regexPattern == null)
{