C#
Uri
programming
.NET
software development

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.

csharp
1using System;
2
3var uri = new Uri("https://example.com/a%20b?q=hello%20world#top");
4Console.WriteLine(uri.AbsoluteUri);

Typical output:

text
https://example.com/a%20b?q=hello%20world#top

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.

csharp
1using System;
2
3var uri = new Uri("https://example.com/a%20b?q=hello%20world#top");
4Console.WriteLine(uri.ToString());

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.

csharp
1using System;
2
3var relative = new Uri("images/logo.png", UriKind.Relative);
4Console.WriteLine(relative.ToString());
5
6try
7{
8    Console.WriteLine(relative.AbsoluteUri);
9}
10catch (InvalidOperationException ex)
11{
12    Console.WriteLine(ex.GetType().Name);
13}

If there is any chance your code may receive relative URIs, calling AbsoluteUri blindly is risky.

Escaping and readability

The easiest mental model is:

  • 'AbsoluteUri favors 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

csharp
1using System;
2
3static void Dump(Uri uri)
4{
5    Console.WriteLine($"Original: {uri.OriginalString}");
6    Console.WriteLine($"ToString: {uri}");
7    Console.WriteLine($"IsAbsoluteUri: {uri.IsAbsoluteUri}");
8
9    if (uri.IsAbsoluteUri)
10    {
11        Console.WriteLine($"AbsoluteUri: {uri.AbsoluteUri}");
12    }
13}
14
15Dump(new Uri("https://example.com/a%20b?q=x%20y"));
16Dump(new Uri("docs/readme.txt", UriKind.Relative));

This kind of diagnostic output is useful when a bug depends on escaping or relative-vs-absolute input.

Common Pitfalls

  • Using AbsoluteUri on a relative URI and then being surprised by an InvalidOperationException.
  • Logging AbsoluteUri and assuming it will be as readable as ToString() 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

  • 'AbsoluteUri returns 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, AbsoluteUri is not safe to call without checking IsAbsoluteUri.
  • Use AbsoluteUri for exact external URL handling and ToString() for display or logging.
  • If you need the caller’s raw input text, look at OriginalString instead.

Course illustration
Course illustration

All Rights Reserved.