Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In .NET 11, several parsing and validation behaviors in Uri and UriBuilder have been tightened to align more strictly with the URI specification and eliminate parsing discrepancies across constructor overloads and factories.
Version introduced
.NET 11 GA
Previous behavior
Previously, the following behaviors were permitted or exhibited:
- Missing IPv6 path delimiter: A URI string without a delimiter between an IPv6 address and the path (such as
"http://[::]extra") was parsed the same as if a delimiter was present ("http://[::]/extra"). UriBuilder.Hostaccepted composite or invalid values: Setting the UriBuilder.Host property allowed values containing path delimiters or user information (such as"contoso.com/path"or"user@host"), potentially altering other URI components when the builder combined values via UriBuilder.Uri or UriBuilder.ToString().- IPv6 length prefixes: Uri allowed parsing of IPv6 length prefixes (such as
"/64"in"http://[AB::/64]/path"). - Embedded IPv4 octal segments: Uri allowed an octal value in the last segment of an IPv4 address embedded within an IPv6 address (for example,
"http://[::1.2.3.04]/"was parsed, whereas"http://[::1.2.03.4]/"was rejected). - Relative URI normalization discrepancy:
new Uri("p%41th", UriKind.Relative)normalized the relative URI input to"pAth", whereasUri.TryCreate("p%41th", UriKind.Relative, out uri)left the value unnormalized as"p%41th".
New behavior
Starting in .NET 11:
- IPv6 path delimiter required: URI parsing enforces that a delimiter (
/) is present after an IPv6 host. Parsing strings like"http://[::]extra"now throws UriFormatException or causes Uri.TryCreate to returnfalse. UriBuilder.Hostvalidates input: The UriBuilder.Host property setter performs best-effort validation and rejects inputs that would alter other URI components. Assigning values such as"contoso.com/path"or"user@host"throws ArgumentException. Certain invalid host formats, such as"[127.0.0.1]", are also caught early.- IPv6 length prefixes rejected: If an IPv6 length prefix is present in the input, URI parsing fails with UriFormatException. Allowed characters in the zone ID (IPv6 scope identifier) are also more strictly constrained.
- Embedded IPv4 octal segments rejected: Octal values are rejected in any segment of an IPv4 address embedded in an IPv6 address. Parsing strings such as
"http://[::1.2.3.04]/"now fails. - Consistent relative URI normalization: Uri(String, UriKind) and TryCreate(String, UriKind, Uri) apply identical normalization rules. Passing
"p%41th"to either API normalizes the value to"pAth".
Type of breaking change
This change is a behavioral change.
Reason for change
These changes enforce stricter adherence to RFC URI standards, improve best-effort validation in UriBuilder to catch inputs that would affect other URI components, and eliminate subtle differences between Uri constructors and Uri.TryCreate.
Recommended action
Most applications will not need changes. However, if your application:
- Uses UriBuilder.Host to set composite values containing paths or user credentials, update the code to assign the individual Path, UserName, or Password properties directly.
- Parses IPv6 URIs without trailing delimiters or with CIDR length prefixes, ensure the input URIs are formatted with a path delimiter and without prefix lengths.