4thex
4thex

Reputation: 1166

Base Uri without a trailing slash

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.

Upvotes: 21

Views: 19426

Answers (2)

Cy Rossignol
Cy Rossignol

Reputation: 16867

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"

Upvotes: 16

eitamal
eitamal

Reputation: 782

You can use the Uri.GetComponents method:

rootUrl.GetComponents(UriComponents.SchemeAndServer, UriFormat.UriEscaped)

Which would return a string representation of the Uri's different components, in this case, UriComponents.SchemeAndServer means the scheme, host, and port components.

You can read more about it on MSDN:

  1. Uri.GetComponents

  2. UriComponents

  3. UriFormat

Upvotes: 5

Related Questions