Skip to content

System.Uri.ToString result is incorrect when combining custom scheme with DOS-style path containing multibyte Unicode character #94572

Closed

Description

@chris-white-wtw

Description

When an instance of Uri is constructed from a string such as "xyz:C:/£", the ToString method produces a string in which the path component incorrectly appears twice: "xyz:C:/£C:/£"

Other properties appear correct though.

Reproduction Steps

Create a console app with the following code:

using System;

var u = new Uri("xyz:C:/£");
Console.WriteLine(u.ToString());

Expected behavior

I'd expect ToString to produce something that Uri's constructor can re-parse to an equivalent Uri instance.

Actual behavior

ToString's result includes the path component of the URI twice. If the original string passed to Uri had escape sequences in then these are unescaped in the second copy of the path but not the first.

Regression?

Behaviour was different in .NET Core 3.1 and in .NET Framework 4.7.2, but also not correct: the result of ToString prepends // to the path (e.g. xyz://C:/£) in these versions of the runtime.

Known Workarounds

Using the .AbsoluteUri property may be an option, though this is escaped differently in general.

Configuration

.NET 6.0.24 / 7.0.5 / 8.0.0-preview.4.23259.5
Windows 10 (19045.3570)
x64

Other information

The bug only seems to occur if all the following are true:

  • A custom URI scheme
  • A path that starts X:/ (where X is any letter) - this is quite specific, in that e.g. X:\ and X|/ do not exhibit the bug
  • The path contains a character that has a multibyte UTF8 representation (whether %-encoded or not)

Activity

  1. ghost added
    needs-area-label
    on Nov 9, 2023
  2. ghost added
    untriaged
    on Nov 9, 2023
  3. added and removed
    needs-area-label
    on Nov 9, 2023
  4. MihaZupan commented on Nov 28, 2023

    @MihaZupan
    Member

    Thank you for the report, the duplication here is indeed a bug.

    Sounds similar to #1061 and #56482

    Console.WriteLine(new Uri("xyz:C:/abc").ToString());
    Console.WriteLine(new Uri("xyz:C:/£abc").ToString());
    Framework, Core 3.1
    xyz:C:/abc
    xyz://C:/£abc
    
    .NET 5, 6, 7, 8
    xyz:C:/abc
    xyz:C:/£abcC:/£abc
    

    We seem to have fixed the // prefix part in .NET 5 but also introduced the duplication. Likely related to #36429.

  5. MihaZupan commented on Nov 28, 2023

    @MihaZupan
    Member

    Triage: It would be good to understand what is happening here and fix it, but

    • This appears to be a rare edge case: custom scheme, file-path-like input, non-ascii, seems to affect only ToString
    • The output for this case was already wrong (different compared to ascii inputs) before net5.0

    Moving to Future, but anyone is welcome to look into the root cause / potential fixes here.

  6. added this to the Future milestone on Nov 28, 2023
  7. ghost removed
    untriaged
    on Nov 28, 2023
  8. self-assigned this
    on Nov 24, 2025
  9. added
    in-pr
    on Nov 24, 2025
  10. modified the milestones: Future, 11.0.0 on Nov 25, 2025
  11. locked and limited conversation to collaborators on Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Netbugin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions