UrlPatternCollection Class

Represents a collection of URL patterns that can be matched against URLs.

Remarks

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.

WHATWG URL Pattern Spec

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

NameDescription
UrlPatternCollection()Initializes a new empty UrlPatternCollection.
UrlPatternCollection(IEnumerable<UrlPattern>)Initializes a new UrlPatternCollection with the specified patterns.

UrlPatternCollection overloads

UrlPatternCollection()

UrlPatternCollection.cs

Initializes a new empty UrlPatternCollection.

public UrlPatternCollection()

UrlPatternCollection(IEnumerable<UrlPattern>)

UrlPatternCollection.cs

Initializes a new UrlPatternCollection with the specified patterns.

public UrlPatternCollection(IEnumerable<UrlPattern> patterns)
Parameters
patterns IEnumerable<UrlPattern>
The patterns to add to the collection.

Properties

NameDescription
CountGets the number of patterns in the collection.

Gets the number of patterns in the collection.

public int Count { get; }

Property value

int

Indexers

NameDescription
this[int]Gets the pattern at the specified index.

Gets the pattern at the specified index.

public UrlPattern this[int index] { get; }

Parameters

index int
The index of the pattern to get.

Property value

UrlPattern

Methods

NameDescription
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

Adds a pattern to the collection.

public void Add(UrlPattern pattern)
Parameters
pattern UrlPattern
The pattern to add.

Add(UrlPatternInit)

UrlPatternCollection.cs

Adds a pattern created from a URL pattern init.

public UrlPattern Add(UrlPatternInit init)
Parameters
init UrlPatternInit
The pattern init.
Returns

UrlPattern The created and added pattern.

Adds a pattern created from a pattern string.

public UrlPattern Add(string pattern)
Parameters
pattern string
The pattern string.
Returns

UrlPattern The created and added pattern.

Add(string, string?)

UrlPatternCollection.cs

Adds a pattern created from a pattern string and base URL.

public UrlPattern Add(string pattern, string? baseUrl)
Parameters
pattern string
The pattern string.
baseUrl string?
The base URL.
Returns

UrlPattern The created and added pattern.

Removes all patterns from the collection.

public void Clear()

Contains(UrlPattern)

UrlPatternCollection.cs

Determines whether the collection contains the specified pattern.

public bool Contains(UrlPattern pattern)

Parameters

pattern UrlPattern
The pattern to locate.

Returns

bool true if the pattern is found; otherwise, false.

FindPattern overloads

FindPattern(string)

UrlPatternCollection.cs

Returns the first pattern in the collection that matches the given URL.

public UrlPattern? FindPattern(string url)
Parameters
url string
The URL to match against.
Returns

UrlPattern? The first matching pattern, or null if no pattern matches.

FindPattern(string, string?)

UrlPatternCollection.cs

Returns the first pattern in the collection that matches the given URL with a base URL.

public UrlPattern? FindPattern(string url, string? baseUrl)
Parameters
url string
The URL to match against.
baseUrl string?
The base URL.
Returns

UrlPattern? The first matching pattern, or null if no pattern matches.

FindPattern(Uri)

UrlPatternCollection.cs

Returns the first pattern in the collection that matches the given URL.

public UrlPattern? FindPattern(Uri url)
Parameters
url Uri
The URL to match against.
Returns

UrlPattern? The first matching pattern, or null if no pattern matches.

Returns 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.cs

Indicates whether any pattern in the collection finds a match in the specified URL input.

public bool IsMatch(UrlPatternInit input)
Parameters
input UrlPatternInit
The URL input to test.
Returns

bool true if any pattern matches; otherwise, false.

Indicates whether any pattern in the collection finds a match in the specified URL.

public bool IsMatch(string url)
Parameters
url string
The URL to test.
Returns

bool true if any pattern matches; otherwise, false.

IsMatch(string, string?)

UrlPatternCollection.cs

Indicates whether any pattern in the collection finds a match in the specified URL with a base URL.

public bool IsMatch(string url, string? baseUrl)
Parameters
url string
The URL to test.
baseUrl string?
The base URL.
Returns

bool true if any pattern matches; otherwise, false.

Indicates whether any pattern in the collection finds a match in the specified URL.

public bool IsMatch(Uri url)
Parameters
url Uri
The URL to test.
Returns

bool true if any pattern matches; otherwise, false.

Match overloads

Match(UrlPatternInit)

UrlPatternCollection.cs

Searches 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
input UrlPatternInit
The URL input to match.
Returns

UrlPatternResult? A UrlPatternResult containing the match result, or null if no pattern matches.

Searches the specified URL using all patterns in the collection and returns the first match result with captured groups.

public UrlPatternResult? Match(string url)
Parameters
url string
The URL to match.
Returns

UrlPatternResult? A UrlPatternResult containing the match result, or null if no pattern matches.

Remarks

Match(string, string?)

UrlPatternCollection.cs

Searches 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
url string
The URL to match.
baseUrl string?
The base URL.
Returns

UrlPatternResult? A UrlPatternResult containing the match result, or null if no pattern matches.

Searches the specified URL using all patterns in the collection and returns the first match result with captured groups.

public UrlPatternResult? Match(Uri url)
Parameters
url Uri
The URL to match.
Returns

UrlPatternResult? A UrlPatternResult containing the match result, or null if no pattern matches.

Remove(UrlPattern)

UrlPatternCollection.cs

Removes a pattern from the collection.

public bool Remove(UrlPattern pattern)

Parameters

pattern UrlPattern
The pattern to remove.

Returns

bool true if the pattern was removed; otherwise, false.

System.Collections.IEnumerable.GetEnumerator()

UrlPatternCollection.cs

Returns an enumerator that iterates through a collection.

IEnumerator IEnumerable.GetEnumerator()

Returns

IEnumerator An IEnumerator object that can be used to iterate through the collection.