UrlPatternCollection Class
Represents a collection of URL patterns that can be matched against URLs.
public sealed class UrlPatternCollection : IEnumerable<UrlPattern>, IReadOnlyCollection<UrlPattern>, IReadOnlyList<UrlPattern>, IEnumerableRemarks
This is the .NET equivalent of the URLPatternList concept from the spec.
Use this class when you need to match a URL against multiple patterns.
Example
// Create a collection of patterns for routing
var patterns = new UrlPatternCollection();
patterns.Add("https://example.com/api/users/:id");
patterns.Add("https://example.com/api/products/:id");
patterns.Add("https://example.com/static/*");
// Check if any pattern matches
Assert.True(patterns.IsMatch("https://example.com/api/users/123"));
// Find the first matching pattern
var matchingPattern = patterns.FindPattern("https://example.com/api/products/456");
Assert.NotNull(matchingPattern);
Assert.Equal("/api/products/:id", matchingPattern.Pathname);
// Get match result with captured groups
var result = patterns.Match("https://example.com/api/users/789");
Assert.NotNull(result);
Assert.Equal("789", result.Pathname.Groups["id"]);- Implements
Constructors
| Name | Description |
|---|---|
| UrlPatternCollection() | Initializes a new empty UrlPatternCollection. |
| UrlPatternCollection(IEnumerable<UrlPattern>) | Initializes a new UrlPatternCollection with the specified patterns. |
UrlPatternCollection overloads
UrlPatternCollection()
UrlPatternCollection.csInitializes a new empty UrlPatternCollection.
public UrlPatternCollection()UrlPatternCollection(IEnumerable<UrlPattern>)
UrlPatternCollection.csInitializes a new UrlPatternCollection with the specified patterns.
public UrlPatternCollection(IEnumerable<UrlPattern> patterns)Parameters
patternsIEnumerable<UrlPattern>- The patterns to add to the collection.
Properties
| Name | Description |
|---|---|
| Count | Gets the number of patterns in the collection. |
Indexers
| Name | Description |
|---|---|
| this[int] | Gets the pattern at the specified index. |
this[int]
UrlPatternCollection.csGets the pattern at the specified index.
public UrlPattern this[int index] { get; }Methods
| Name | Description |
|---|---|
| Add(UrlPattern) | Adds a pattern to the collection. |
| Add(UrlPatternInit) | Adds a pattern created from a URL pattern init. |
| Add(string) | Adds a pattern created from a pattern string. |
| Add(string, string?) | Adds a pattern created from a pattern string and base URL. |
| Clear() | Removes all patterns from the collection. |
| Contains(UrlPattern) | Determines whether the collection contains the specified pattern. |
| FindPattern(string) | Returns the first pattern in the collection that matches the given URL. |
| FindPattern(string, string?) | Returns the first pattern in the collection that matches the given URL with a base URL. |
| FindPattern(Uri) | Returns the first pattern in the collection that matches the given URL. |
| GetEnumerator() | Returns an enumerator that iterates through the collection. |
| IsMatch(UrlPatternInit) | Indicates whether any pattern in the collection finds a match in the specified URL input. |
| IsMatch(string) | Indicates whether any pattern in the collection finds a match in the specified URL. |
| IsMatch(string, string?) | Indicates whether any pattern in the collection finds a match in the specified URL with a base URL. |
| IsMatch(Uri) | Indicates whether any pattern in the collection finds a match in the specified URL. |
| Match(UrlPatternInit) | Searches the specified URL input using all patterns in the collection and returns the first match result with captured groups. |
| Match(string) | Searches the specified URL using all patterns in the collection and returns the first match result with captured groups. |
| Match(string, string?) | Searches the specified URL with a base URL using all patterns in the collection and returns the first match result with captured groups. |
| Match(Uri) | Searches the specified URL using all patterns in the collection and returns the first match result with captured groups. |
| Remove(UrlPattern) | Removes a pattern from the collection. |
| System.Collections.IEnumerable.GetEnumerator() | Returns an enumerator that iterates through a collection. |
Add overloads
Add(UrlPattern)
UrlPatternCollection.csAdds a pattern to the collection.
public void Add(UrlPattern pattern)Parameters
patternUrlPattern- The pattern to add.
Add(UrlPatternInit)
UrlPatternCollection.csAdds a pattern created from a URL pattern init.
public UrlPattern Add(UrlPatternInit init)Add(string)
UrlPatternCollection.csAdds a pattern created from a pattern string.
public UrlPattern Add(string pattern)Add(string, string?)
UrlPatternCollection.csAdds a pattern created from a pattern string and base URL.
public UrlPattern Add(string pattern, string? baseUrl)Clear()
UrlPatternCollection.csRemoves all patterns from the collection.
public void Clear()Contains(UrlPattern)
UrlPatternCollection.csDetermines whether the collection contains the specified pattern.
public bool Contains(UrlPattern pattern)Parameters
patternUrlPattern- The pattern to locate.
Returns
bool true if the pattern is found; otherwise, false.
FindPattern overloads
FindPattern(string)
UrlPatternCollection.csReturns the first pattern in the collection that matches the given URL.
public UrlPattern? FindPattern(string url)Parameters
urlstring- The URL to match against.
Returns
UrlPattern? The first matching pattern, or null if no pattern matches.
FindPattern(string, string?)
UrlPatternCollection.csReturns the first pattern in the collection that matches the given URL with a base URL.
public UrlPattern? FindPattern(string url, string? baseUrl)FindPattern(Uri)
UrlPatternCollection.csReturns the first pattern in the collection that matches the given URL.
public UrlPattern? FindPattern(Uri url)Parameters
urlUri- The URL to match against.
Returns
UrlPattern? The first matching pattern, or null if no pattern matches.
GetEnumerator()
UrlPatternCollection.csReturns an enumerator that iterates through the collection.
public IEnumerator<UrlPattern> GetEnumerator()Returns
IEnumerator<UrlPattern> An enumerator that can be used to iterate through the collection.
IsMatch overloads
IsMatch(UrlPatternInit)
UrlPatternCollection.csIndicates whether any pattern in the collection finds a match in the specified URL input.
public bool IsMatch(UrlPatternInit input)Parameters
inputUrlPatternInit- The URL input to test.
Returns
bool true if any pattern matches; otherwise, false.
IsMatch(string)
UrlPatternCollection.csIsMatch(string, string?)
UrlPatternCollection.csIsMatch(Uri)
UrlPatternCollection.csMatch overloads
Match(UrlPatternInit)
UrlPatternCollection.csSearches the specified URL input using all patterns in the collection and returns the first match result with captured groups.
public UrlPatternResult? Match(UrlPatternInit input)Parameters
inputUrlPatternInit- The URL input to match.
Returns
UrlPatternResult? A UrlPatternResult containing the match result, or null if no pattern matches.
Match(string)
UrlPatternCollection.csSearches the specified URL using all patterns in the collection and returns the first match result with captured groups.
public UrlPatternResult? Match(string url)Parameters
urlstring- The URL to match.
Returns
UrlPatternResult? A UrlPatternResult containing the match result, or null if no pattern matches.
Remarks
Match(string, string?)
UrlPatternCollection.csSearches the specified URL with a base URL using all patterns in the collection and returns the first match result with captured groups.
public UrlPatternResult? Match(string url, string? baseUrl)Parameters
Returns
UrlPatternResult? A UrlPatternResult containing the match result, or null if no pattern matches.
Match(Uri)
UrlPatternCollection.csSearches the specified URL using all patterns in the collection and returns the first match result with captured groups.
public UrlPatternResult? Match(Uri url)Parameters
urlUri- The URL to match.
Returns
UrlPatternResult? A UrlPatternResult containing the match result, or null if no pattern matches.
Remove(UrlPattern)
UrlPatternCollection.csRemoves a pattern from the collection.
public bool Remove(UrlPattern pattern)Parameters
patternUrlPattern- The pattern to remove.
Returns
bool true if the pattern was removed; otherwise, false.
System.Collections.IEnumerable.GetEnumerator()
UrlPatternCollection.csReturns an enumerator that iterates through a collection.
IEnumerator IEnumerable.GetEnumerator()Returns
IEnumerator An IEnumerator object that can be used to iterate through the collection.