Skip to content

Uri doesn't expose the original, user-provided port value #89768

@roji

Description

@roji

When a port isn't specified, Uri automatically infers the default port based on the scheme:

Console.WriteLine(new Uri("http://example.com").Port); // 80
Console.WriteLine(new Uri("https://example.com").Port); // 443

However, Uri doesn't provide any way to know whether that port was explicitly user-specified, or whether Uri inferred it. This presents a problem when using Uri to accept endpoint information from users for services which by default run on the non-standard port.

For example, if my custom service by default runs over HTTP on port 12345, and the user omits the port, Uri will return 80 as the HTTP default. If I assume that 80 means the user hasn't provided a port and default to 12345, it becomes impossible for the user to run on port 80 if they so wish.

Of course, it's possible to do various string matching to see whether :80 was present, but it seems like Uri should provide some way to access the user-provided port, or to know whether the port was inferred or not.

/cc @stephentoub

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Net

    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