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
When a port isn't specified, Uri automatically infers the default port based on the scheme:
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
:80was 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