question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uri.AbsoluteUri should be used instead of Uri.ToString()

See original GitHub issue

There are multiple customer-reported issues where the root cause is incorrect usage of Uri.ToString():

The problem is Uri.ToString() un-escapes the URL, so http://foo.com/image%201.jpg is converted to http://foo.com/image 01.jpg which is no longer a valid URL. The API Uri.AbsoluteUri should be used instead.

All instances of Uri.ToString() in our codebase should be reviewed, and most or all should be convered to Uri.AbsoluteUri. Uri.ToString() should only be used to intentionally un-escape a URL, which should be rare. Even if a URL were included in an error message, I think the escaped URL would be preferred.

I recommend adding an analyzer rule to prevent calls to Uri.ToString(), using Microsoft.CodeAnalysis.BannedApiAnalyzers.

One complication is relative URIs, where Uri.AbsoluteUri throws. If we need to get an escaped representation of a relative URI, we can use Uri.EscapeUriString(relativeUri.ToString()).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kasobol-msftcommented, Jun 3, 2021

Given how tempting ToString() is and how easy it is to get it wrong I suggest to prioritize this work.

0reactions
pakrymcommented, Jul 2, 2021

I’ll look into merging that PR today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What's the difference between Uri.ToString() and ...
AbsoluteUri notes that it "Gets the absolute URI", Uri.ToString() "Gets a canonical string representation for the specified instance." c# .net.
Read more >
Uri.AbsoluteUri Property (System)
This instance represents a relative URI, and this property is valid only for absolute URIs. Examples. The following example writes the complete contents...
Read more >
Uri.AbsoluteUri and Uri.ToString() are NOT the same - JMPInline
AbsoluteUri instead of Uri.ToString(). Incidentally, UriBuilder.ToString() does not decode the URL, so UriBuilder.ToString() is safe to use.
Read more >
URI (Java Platform SE 8 )
An absolute URI specifies a scheme; a URI that is not absolute is said to be relative. URIs are also classified according to...
Read more >
c# - Truncate port number from absolute Uri
The right solution here is to use UriBuilder as follows. var uri = new UriBuilder(baseUri); string newUrl = uri.Uri.ToString();.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found