Runtime: Remove the Trailing Slash from URI when converting to String

Created on 26 Aug 2020  路  7Comments  路  Source: dotnet/runtime

In Asp.net core 3.1, I create a Uri like below:

Uri uri = new Uri($"https://www.example.com?id=1&name=aa");

When I convert it to string:

uri.ToString()

It will automatically add a trailing slash behind the domain:

https://www.example.com/?id=1&name=aa

Is there a way to remove the trailing slash?

area-System.Net question

All 7 comments

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Try uri.OriginalString:

```c#
Uri uri = new Uri($"https://www.example.com?id=1&name=aa");
Console.WriteLine(uri.ToString());
Console.WriteLine(uri.AbsoluteUri);
Console.WriteLine(uri.OriginalString);

Output:

https://www.example.com/?id=1&name=aa
https://www.example.com/?id=1&name=aa
https://www.example.com?id=1&name=aa
```

What is the goal behind removing this slash?

I'm just curious why this slash is automatically added, seems it is the default behavior.

@mjd575 I believe the slash is required by the Uri RFC. Without slash, it is not well formed IMO -- @MihaZupan will be able to say for sure.

I believe it may have been required by ancient Url specifications, though not required by the current spec. So having the slash there can theoretically improve compatibility.

Chrome will for example add slash when making a request. See http://example.com?foo=bar

I don't see any value in omitting it - is there a use-case behind it, or just curiosity?

Omitting the slash could make sense for distinguishing an OPTIONS * request (RFC 7230 搂5.3.4) from OPTIONS /, as hinted by the URI normalization rule (RFC 7230 搂2.7.3). HttpConnection.SendAsyncCore in SocketsHttpHandler does not seem to support that, though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GitAntoinee picture GitAntoinee  路  3Comments

EgorBo picture EgorBo  路  3Comments

nalywa picture nalywa  路  3Comments

matty-hall picture matty-hall  路  3Comments

omajid picture omajid  路  3Comments