Edit

Share via


Uri length limits removed

Methods that create Uri instances (constructors and TryCreate factory methods) have historically limited the length of the URI string to around 65,000 characters (exact limits varied slightly depending on the input format). These limits have been lifted such that there is practically no upper bound on how long Uri instances can be.

Version introduced

.NET 10 Preview 7

Previous behavior

Previously, it wasn't possible to create a Uri instance whose length exceeded around 65,000 characters. Code like the following example threw a UriFormatException with the message "Invalid URI: The Uri string is too long."

new Uri($"https://host/{new string('a', 100_000)}")

New behavior

Starting in .NET 10, Uri instances containing large amounts of data can now be created. For example:

string largeQuery = ...;
return new Uri($"https://someService/?query={Uri.EscapeDataString(largeQuery)}");

The removed restrictions mainly apply to paths, queries, and fragments as the most practical components to carry large amounts of data. Components such as the scheme and host might still enforce some length limits. Practical limitations as you approach the length limits of string also apply, so you can't (nor should you) use Uri to represent a 10 GB file.

Type of breaking change

This change is a behavioral change.

Reason for change

Most HTTP servers enforce strict length restrictions on URLs they are willing to accept in requests. The limits are generally much lower than Uri's previous limits. However, since Uri is the de facto exchange type in .NET for URI-like information, the previous limits limited its use in some scenarios without good workarounds aside from removing the use of Uri throughout API contracts.

The main scenarios for large Uri are:

  • data: uris, which contain arbitrary binary blobs encoded in Base64. These might be transmitted outside of the HTTP request line. For example, they might be part of the request body, and can therefore be arbitrarily large. Uri can now be used to represent data URIs containing larger files.
  • Large query strings. Uri is often used as an exchange type between systems even if it will never be sent as part of an HTTP request. User request information is often encoded as part of the query string, so the new behavior enables such scenarios even as the amount of data grows.

For most users, there is no action required.

If you rely on Uri to impose length restrictions as part of your input validation, you must now perform length checking yourself, preferably as a step before you construct the Uri instance. As most HTTP servers enforce much stricter length limits in practice, very long inputs were already likely to result in failures when sent as part of an HTTP request. You might find that your scenario would benefit from performing even stricter length validation than Uri had done previously.

Affected APIs