UrlPattern Class

A URL pattern (https://urlpattern.spec.whatwg.org/#urlpattern): matches URLs, or the components of URLs, against patterns using the syntax of path-to-regexp (e.g. /books/:id(\\d+), https://*.example.com/*).

public sealed class UrlPattern

Constructors

NameDescription
UrlPattern(UrlPatternInput, UrlPatternOptions?)Creates a pattern from a pattern string (an absolute URL pattern) or from a UrlPatternInit (the missing components match anything).
UrlPattern(UrlPatternInput, string, UrlPatternOptions?)Creates a pattern from a pattern string, relative to a base URL, or from a UrlPatternInit (then baseUrl must not be given).

UrlPattern overloads

UrlPattern(UrlPatternInput, UrlPatternOptions?)

UrlPattern.cs

Creates a pattern from a pattern string (an absolute URL pattern) or from a UrlPatternInit (the missing components match anything).

public UrlPattern(UrlPatternInput input = default, UrlPatternOptions? options = null)

UrlPattern(UrlPatternInput, string, UrlPatternOptions?)

UrlPattern.cs

Creates a pattern from a pattern string, relative to a base URL, or from a UrlPatternInit (then baseUrl must not be given).

public UrlPattern(UrlPatternInput input, string baseUrl, UrlPatternOptions? options = null)

Properties

NameDescription
HasRegExpGroupsWhether a component of the pattern uses a custom regular expression (e.g. :id(\\d+)).
HashThe normalized hash pattern string.
HostnameThe normalized hostname pattern string.
PasswordThe normalized password pattern string.
PathnameThe normalized pathname pattern string.
PortThe normalized port pattern string.
ProtocolThe normalized protocol pattern string.
SearchThe normalized search pattern string.
UsernameThe normalized username pattern string.

HasRegExpGroups

UrlPattern.cs

Whether a component of the pattern uses a custom regular expression (e.g. :id(\\d+)).

public bool HasRegExpGroups { get; }

The normalized hash pattern string.

public string Hash { get; }

The normalized hostname pattern string.

public string Hostname { get; }

The normalized password pattern string.

public string Password { get; }

The normalized pathname pattern string.

public string Pathname { get; }

The normalized port pattern string.

public string Port { get; }

The normalized protocol pattern string.

public string Protocol { get; }

The normalized username pattern string.

public string Username { get; }

Methods

NameDescription
CompareComponent(string, UrlPattern, UrlPattern)Compares the specificity of one component of two URL patterns.
Exec(Url)Matches a URL and returns the matched groups, or null if the pattern does not match.
Exec(UrlPatternInput, string?)Matches a URL (resolved against baseUrl) or the components of a UrlPatternInit, and returns the matched groups, or null if the pattern does not match.
Generate(string, IEnumerable<KeyValuePair<string, string>>)Generates the string for one URL component from the pattern and the values of its named groups.
Test(Url)Returns whether the pattern matches a URL.
Test(UrlPatternInput, string?)Returns whether the pattern matches a URL (resolved against baseUrl) or the components of a UrlPatternInit.

CompareComponent(string, UrlPattern, UrlPattern)

UrlPattern.cs
  • Experimental (WEBPLATEXP0015)

Compares the specificity of one component of two URL patterns.

[Experimental("WEBPLATEXP0015", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public static int CompareComponent(string component, UrlPattern left, UrlPattern right)

Parameters

component string
The URL component to compare.
left UrlPattern
The first pattern.
right UrlPattern
The second pattern.

Returns

int A negative number when left sorts before right, zero when they are equivalent, or a positive number otherwise.

Exceptions

TypeErrorException
component is not a URLPattern component.

See also

Exec overloads

Exec(Url)

UrlPattern.cs

Matches a URL and returns the matched groups, or null if the pattern does not match.

public UrlPatternResult? Exec(Url url)
Parameters
url Url
Returns

UrlPatternResult?

Exec(UrlPatternInput, string?)

UrlPattern.cs

Matches a URL (resolved against baseUrl) or the components of a UrlPatternInit, and returns the matched groups, or null if the pattern does not match.

public UrlPatternResult? Exec(UrlPatternInput input = default, string? baseUrl = null)

Generate(string, IEnumerable<KeyValuePair<string, string>>)

UrlPattern.cs
  • Experimental (WEBPLATEXP0015)

Generates the string for one URL component from the pattern and the values of its named groups.

[Experimental("WEBPLATEXP0015", UrlFormat = "https://github.com/meziantou/Meziantou.Framework/blob/main/src/Meziantou.Framework.WebPlatform/docs/experimental-apis.md#{0}")]
public string Generate(string component, IEnumerable<KeyValuePair<string, string>> groups)

Parameters

component string
The URL component to generate.
groups IEnumerable<KeyValuePair<string, string>>
The group names and values.

Returns

string The generated component value.

Exceptions

TypeErrorException
The component is invalid, a required group is missing, or the component uses unsupported pattern syntax.

See also

Test overloads

Test(Url)

UrlPattern.cs

Returns whether the pattern matches a URL.

public bool Test(Url url)
Parameters
url Url
Returns

bool

Test(UrlPatternInput, string?)

UrlPattern.cs

Returns whether the pattern matches a URL (resolved against baseUrl) or the components of a UrlPatternInit.

public bool Test(UrlPatternInput input = default, string? baseUrl = null)