Skip to content

System.UriBuilder cannot build a correct mailto URI #87446

@jrdodds

Description

@jrdodds

Description

The System.UriBuilder class cannot be used to build a mailto URI.

Reproduction Steps

The following code

var builder = new UriBuilder
{
    Scheme = Uri.UriSchemeMailto,
    UserName = "someone",
    Host = "example.com",
    Query = "subject=Testing",
    Path = string.Empty,
};
Console.WriteLine(builder.ToString());

produces a URI value of

mailto:someone@example.com/?subject=Testing

which is not a valid mailto URI.

Expected behavior

A valid URI would be

mailto:someone@example.com?subject=Testing

There should not be a '/' character.

Actual behavior

The Path setter sets the value to '/' when the value is the empty string.

    [AllowNull]
    public string Path
    {
        get => _path;
        set
        {
            _path = string.IsNullOrEmpty(value)
                ? "/"
                : Uri.InternalEscapeString(value.Replace('\\', '/'));
            _changed = true;
        }
    }

This makes sense for HTTP/S but makes the class unusable for mailto.

Regression?

No response

Known Workarounds

No response

Configuration

.Net 7.0.203
Windows 11 x64

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions