Summary#
Uri is the parsed-URL handle, spelled exactly as in C#. Construct it from a URL string and read its parts:
var u = new Uri("https://api.example.com:8443/v1/users?active=true#top");
u.Scheme; // "https"
u.Host; // "api.example.com"
u.Port; // 8443
u.AbsolutePath; // "/v1/users"
u.PathAndQuery; // "/v1/users?active=true"
u.Query; // "?active=true"
u.Fragment; // "#top"It is pure — no capability, no using required. Uri is also where the URL-escaping helpers live
(Encoding — Base64, URL, HTML — Uri.EscapeDataString), exactly as C#'s System.Uri carries both.
Signature#
Uri new Uri(string url) // parse an ABSOLUTE URL
string uri.Scheme // "https"
string uri.Host // "api.example.com"
int uri.Port // 8443 (or the scheme default: 443 for https, 80 for http)
string uri.AbsolutePath // "/v1/users"
string uri.PathAndQuery // "/v1/users?active=true"
string uri.Query // "?active=true" (empty when there is none)
string uri.Fragment // "#top" (empty when there is none)Description#
new Uri(url) parses an absolute URL once and exposes its components as instance members — the same shape as C#'s
System.Uri. When the URL omits the port, .Port is the scheme's default (443 for https, 80 for http). .Query
and .Fragment include their leading ? / #, and are empty strings when absent.
An invalid or relative URL throws — new Uri parses an absolute URI, faithful to C#. Validate untrusted input
first (e.g. with Regex) if a throw is not what you want.
Examples#
Route on the host of a webhook URL, parsing an Http.* target:
bool IsTrustedHost(string webhookUrl) {
var u = new Uri(webhookUrl);
return u.Scheme == "https" && Text.EndsWith(u.Host, ".example.com");
}Build a URL with an escaped query value (Encoding — Base64, URL, HTML) and read it back:
Uri Search(string term) {
return new Uri("https://api.example.com/search?q=" + Uri.EscapeDataString(term));
}See also#
- Encoding — Base64, URL, HTML —
Uri.EscapeDataString/UnescapeDataStringfor building the URL you parse - Http.* — the outbound HTTP surface whose URLs this parses
- Regex — validate a URL string before parsing untrusted input