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 UrlPatternConstructors
| Name | Description |
|---|---|
| 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.csCreates 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)Parameters
inputUrlPatternInputoptionsUrlPatternOptions?
Exceptions
TypeErrorException- The pattern is not valid.
See also
UrlPattern(UrlPatternInput, string, UrlPatternOptions?)
UrlPattern.csCreates 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)Parameters
inputUrlPatternInputbaseUrlstringoptionsUrlPatternOptions?
Exceptions
TypeErrorException- The pattern is not valid.
See also
Properties
| Name | Description |
|---|---|
| HasRegExpGroups | Whether a component of the pattern uses a custom regular expression (e.g. :id(\\d+)). |
| Hash | The normalized hash pattern string. |
| Hostname | The normalized hostname pattern string. |
| Password | The normalized password pattern string. |
| Pathname | The normalized pathname pattern string. |
| Port | The normalized port pattern string. |
| Protocol | The normalized protocol pattern string. |
| Search | The normalized search pattern string. |
| Username | The normalized username pattern string. |
HasRegExpGroups
UrlPattern.csWhether a component of the pattern uses a custom regular expression (e.g. :id(\\d+)).
public bool HasRegExpGroups { get; }Hash
UrlPattern.csThe normalized hash pattern string.
public string Hash { get; }Hostname
UrlPattern.csThe normalized hostname pattern string.
public string Hostname { get; }Password
UrlPattern.csThe normalized password pattern string.
public string Password { get; }Pathname
UrlPattern.csThe normalized pathname pattern string.
public string Pathname { get; }Port
UrlPattern.csThe normalized port pattern string.
public string Port { get; }Protocol
UrlPattern.csThe normalized protocol pattern string.
public string Protocol { get; }Search
UrlPattern.csThe normalized search pattern string.
public string Search { get; }Username
UrlPattern.csThe normalized username pattern string.
public string Username { get; }Methods
| Name | Description |
|---|---|
| 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
componentstring- The URL component to compare.
leftUrlPattern- The first pattern.
rightUrlPattern- The second pattern.
Returns
int A negative number when left sorts before right, zero when they are equivalent, or a positive number otherwise.
Exceptions
TypeErrorExceptioncomponentis not a URLPattern component.
See also
Exec overloads
Exec(Url)
UrlPattern.csMatches a URL and returns the matched groups, or null if the pattern does not match.
public UrlPatternResult? Exec(Url url)Exec(UrlPatternInput, string?)
UrlPattern.csMatches 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)Parameters
inputUrlPatternInputbaseUrlstring?
Returns
Exceptions
TypeErrorExceptionbaseUrlis given with aUrlPatternInit.
See also
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
componentstring- The URL component to generate.
groupsIEnumerable<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.csTest(UrlPatternInput, string?)
UrlPattern.csReturns whether the pattern matches a URL (resolved against baseUrl) or the components of a UrlPatternInit.
public bool Test(UrlPatternInput input = default, string? baseUrl = null)Parameters
inputUrlPatternInputbaseUrlstring?
Returns
Exceptions
TypeErrorExceptionbaseUrlis given with aUrlPatternInit.