Skip to content

Commit 1078da9

Browse files
feat : Add StringSyntax for regex parameters (#40589)
* feat : Add `StringSyntax` for regex parameters * add missing using statement. * addressed PR feedback * Update RuleBuilder.cs * removed usage from test utils Co-authored-by: Javier Calvarro Nelson <[email protected]>
1 parent 8d6d88e commit 1078da9

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/Http/Routing/src/Constraints/RegexInlineRouteConstraint.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Text.RegularExpressions;
6+
47
namespace Microsoft.AspNetCore.Routing.Constraints;
58

69
/// <summary>
@@ -12,7 +15,7 @@ public class RegexInlineRouteConstraint : RegexRouteConstraint
1215
/// Initializes a new instance of the <see cref="RegexInlineRouteConstraint" /> class.
1316
/// </summary>
1417
/// <param name="regexPattern">The regular expression pattern to match.</param>
15-
public RegexInlineRouteConstraint(string regexPattern)
18+
public RegexInlineRouteConstraint([StringSyntax(StringSyntaxAttribute.Regex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)] string regexPattern)
1619
: base(regexPattern)
1720
{
1821
}

src/Middleware/Rewrite/src/RewriteOptionsExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Microsoft.AspNetCore.Http;
56

67
namespace Microsoft.AspNetCore.Rewrite;
@@ -42,7 +43,7 @@ public static RewriteOptions Add(this RewriteOptions options, Action<RewriteCont
4243
/// <param name="replacement">If the regex matches, what to replace the uri with.</param>
4344
/// <param name="skipRemainingRules">If the regex matches, conditionally stop processing other rules.</param>
4445
/// <returns>The Rewrite options.</returns>
45-
public static RewriteOptions AddRewrite(this RewriteOptions options, string regex, string replacement, bool skipRemainingRules)
46+
public static RewriteOptions AddRewrite(this RewriteOptions options, [StringSyntax(StringSyntaxAttribute.Regex)] string regex, string replacement, bool skipRemainingRules)
4647
{
4748
options.Rules.Add(new RewriteRule(regex, replacement, skipRemainingRules));
4849
return options;
@@ -55,7 +56,7 @@ public static RewriteOptions AddRewrite(this RewriteOptions options, string rege
5556
/// <param name="regex">The regex string to compare with.</param>
5657
/// <param name="replacement">If the regex matches, what to replace the uri with.</param>
5758
/// <returns>The Rewrite options.</returns>
58-
public static RewriteOptions AddRedirect(this RewriteOptions options, string regex, string replacement)
59+
public static RewriteOptions AddRedirect(this RewriteOptions options, [StringSyntax(StringSyntaxAttribute.Regex)] string regex, string replacement)
5960
{
6061
return AddRedirect(options, regex, replacement, statusCode: StatusCodes.Status302Found);
6162
}
@@ -68,7 +69,7 @@ public static RewriteOptions AddRedirect(this RewriteOptions options, string reg
6869
/// <param name="replacement">If the regex matches, what to replace the uri with.</param>
6970
/// <param name="statusCode">The status code to add to the response.</param>
7071
/// <returns>The Rewrite options.</returns>
71-
public static RewriteOptions AddRedirect(this RewriteOptions options, string regex, string replacement, int statusCode)
72+
public static RewriteOptions AddRedirect(this RewriteOptions options, [StringSyntax(StringSyntaxAttribute.Regex)] string regex, string replacement, int statusCode)
7273
{
7374
options.Rules.Add(new RedirectRule(regex, replacement, statusCode));
7475
return options;

0 commit comments

Comments
 (0)