What's the difference between Uri.ToString and Uri.AbsoluteUri?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Uri.ToString() and Uri.AbsoluteUri both give you string forms of a Uri, but they are not interchangeable. In .NET, AbsoluteUri is the fully escaped absolute representation, while ToString() is a friendlier textual form that may unescape some characters and is also available on relative URIs.
Core Sections
What AbsoluteUri returns
AbsoluteUri is a property for absolute URIs only. It returns the canonical absolute string, including the scheme, host, path, query, and fragment, in escaped form.
Typical output:
This is the representation you usually want when generating HTTP requests, persisting exact URLs, or comparing normalized absolute addresses.
What ToString() returns
ToString() produces a display-oriented string. It may unescape characters where .NET considers that safe and readable.
Depending on the URI contents, this may print a string with spaces and other readable characters instead of preserving every escape sequence exactly as in AbsoluteUri.
That makes ToString() useful for logging and diagnostics, but less ideal when you need an exact wire-format address.
Relative URI behavior is a major difference
One practical difference is that ToString() works on relative URIs, while AbsoluteUri does not.
If there is any chance your code may receive relative URIs, calling AbsoluteUri blindly is risky.
Escaping and readability
The easiest mental model is:
- '
AbsoluteUrifavors correctness and escaping' - '
ToString()favors readability'
For example, if the URI contains spaces, internationalized names, or already escaped components, AbsoluteUri is the safer choice when another system will parse the result again. ToString() is more appropriate when a human is the reader.
If you need the exact original text as supplied by the caller, there is another property to be aware of: OriginalString. That is a different concern again, because neither ToString() nor AbsoluteUri is intended as “the exact raw user input” in every case.
When each one is appropriate
Use AbsoluteUri when:
- you need an absolute URI only
- you need the escaped canonical form
- the result will be sent to another system or persisted as a URL value
Use ToString() when:
- you are logging or showing the value to a user
- the URI may be relative
- readability matters more than exact escaped formatting
Example comparison helper
This kind of diagnostic output is useful when a bug depends on escaping or relative-vs-absolute input.
Common Pitfalls
- Using
AbsoluteUrion a relative URI and then being surprised by anInvalidOperationException. - Logging
AbsoluteUriand assuming it will be as readable asToString()when escaped characters are present. - Using
ToString()for protocol-sensitive comparisons or outbound requests where the exact escaped form matters. - Confusing both APIs with
OriginalString, which answers a different question about the original input text. - Assuming the two members always return identical output just because they often look the same for simple URLs.
Summary
- '
AbsoluteUrireturns the escaped canonical form of an absolute URI.' - '
ToString()returns a friendlier string and also works for relative URIs.' - If the URI may be relative,
AbsoluteUriis not safe to call without checkingIsAbsoluteUri. - Use
AbsoluteUrifor exact external URL handling andToString()for display or logging. - If you need the caller’s raw input text, look at
OriginalStringinstead.

