If I create a Uri
using the UriBuilder
like this:
var rootUrl = new UriBuilder("http", "example.com", 50000).Uri;
then the AbsoluteUri
of rootUrl
always contain a trailing slash like this:
http://example.com:50000/
What I would like, is to create a Uri
object without the trailing slash, but it seems impossible.
My workaround is to store it as a string instead, and do something ugly like this:
var rootUrl = new UriBuilder("http", "example.com", 50000).Uri.ToString().TrimEnd('/');
I have heard people say that without the trailing slash, the Uri is invalid. I don't think that is true. I have looked through RFC 3986, and in section 3.2.2 it says:
If a URI contains an authority component, then the path component must either be empty or begin with a slash ("/") character.
It doesn't say that the trailing slash has to be there.
It does not matter if your root or hostname has a trailing slash or not. However, different browsers may sometimes show the URL as either having a trailing slash or not when you look at the address bar.
A trailing slash at the end of a URL on your website can cause issues with duplicate content if not dealt with correctly. Put simply, Google doesn't like seeing the same content on different pages. It can be confusing for both search engines and users.
When using APPEND_SLASH , if they accidently sent it without trailing slash, and your urlconf is WITH a trailing slash they would get an exception about data lose when redirecting POST requests.
Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.
The trailing slash is not required in an arbitrary URI, but it is the part of the canonical representation of an absolute URI for requests in HTTP:
Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as "/" (the server root).
To adhere to the spec, the Uri
class outputs a URI in the form with a trailing slash:
In general, a URI that uses the generic syntax for authority with an empty path should be normalized to a path of "/".
This behavior is not configurable on a Uri
object in .NET. Web browsers and many HTTP clients perform the same rewriting when sending requests for URLs with an empty path.
If we want to internally represent our URL as a Uri
object, not a string, we can create an extension method that formats the URL without the trailing slash, which abstracts this presentation logic in one location instead of duplicating it every time we need to output the URL for display:
namespace Example.App.CustomExtensions
{
public static class UriExtensions
{
public static string ToRootHttpUriString(this Uri uri)
{
if (!uri.IsHttp())
{
throw new InvalidOperationException(...);
}
return uri.Scheme + "://" + uri.Authority;
}
public static bool IsHttp(this Uri uri)
{
return uri.Scheme == "http" || uri.Scheme == "https";
}
}
}
Then:
using Example.App.CustomExtensions;
...
var rootUrl = new UriBuilder("http", "example.com", 50000).Uri;
Console.WriteLine(rootUrl.ToRootHttpUriString()); // "http://example.com:50000"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With