Getting started
editInstall the package
Add the Meziantou.Framework.Uri package to a project:
dotnet package add Meziantou.Framework.UriThe examples of this documentation omit the using directives: they need using Meziantou.Framework;, and using Microsoft.Extensions.Primitives; for the examples that use StringValues.
Match a URL against a pattern
UrlPattern.Create parses a pattern. IsMatch tests a URL, and Match returns the values of the groups:
// Create a pattern and test if a URL matches
var pattern = UrlPattern.Create("https://example.com/users/:id");
Assert.True(pattern.IsMatch("https://example.com/users/123"));
// Extract captured groups from a URL
var result = pattern.Match("https://example.com/users/456");
Assert.NotNull(result);
Assert.Equal("456", result.Pathname.Groups["id"]);
// Use wildcards to match any value
var wildcardPattern = UrlPattern.Create("https://*.example.com/*");
Assert.True(wildcardPattern.IsMatch("https://api.example.com/data"));:idis a named group: it matches one path segment, and its value is in the result.*is a wildcard: it matches any text.
See Matching URLs and Pattern syntax.
Read and edit a query string
QueryStringUtilities parses a query string and returns a new URL with parameters added, replaced or removed:
var query = QueryStringUtilities.ParseQuery("?author=meziantou&tag=dotnet&tag=csharp");
Assert.Equal("meziantou", query["author"].ToString());
Assert.Equal(["dotnet", "csharp"], query["tag"].ToArray());
var uri = "https://www.meziantou.net/?author=meziantou";
Assert.Equal("https://www.meziantou.net/?author=meziantou&name=value", QueryStringUtilities.AddQueryString(uri, "name", "value"));
Assert.Equal("https://www.meziantou.net/?author=john", QueryStringUtilities.AddOrReplaceQueryString(uri, "author", "john"));
// Replace the whole query string
Assert.Equal("https://www.meziantou.net/?name=value", QueryStringUtilities.SetQueryString(uri, [KeyValuePair.Create("name", (string?)"value")]));
// Remove a query string parameter
Assert.Equal("https://www.meziantou.net/", QueryStringUtilities.RemoveQueryString(uri, "author"));See Query strings.
Compare two sites
PublicSuffixList.GetRegistrableDomain returns the part of a host name that a registrant controls, so two hosts with the same registrable domain belong to the same site:
static bool IsSameSite(string host1, string host2)
{
var site1 = PublicSuffixList.GetRegistrableDomain(host1);
var site2 = PublicSuffixList.GetRegistrableDomain(host2);
return site1 is not null && site1 == site2;
}
Assert.True(IsSameSite("www.example.co.uk", "shop.example.co.uk"));
Assert.False(IsSameSite("example.co.uk", "contoso.co.uk"));
// github.io is a public suffix: two sites hosted there are different sites
Assert.False(IsSameSite("alice.github.io", "bob.github.io"));See Public Suffix List.
Next steps
- Matching URLs: create patterns, match strings,
Urivalues and URL components, read the result. - Pattern collections: find which pattern of a list matches a URL.
- Limitations: what the library does not do.