Sphyrnidae Common Library  2.0.1
Shared Utilities/Library
Url and Path Builder

Overview

There are 2 classes for you to use:

  1. UrlBuilder
  2. RelativePathBuilder

Each of these classes utilize the builder pattern. Behind the scenes, the UriBuilder is used to perform most of the work. What these individual classes offer is some enhanced capabilities and safer usage (eg. not needing to check for slashes "/").

Where Used

None

Examples

    // To generate the following URL: https://www.foo.com:123/a/b/c?attr1=1&attr2=2#gohere
    var url = new UrlBuilder()
        .AsHttps()
        .WithHost("www.foo.com")
        .WithPort(123)
        .AddPathSegment("a")
        .AddPathSegment("b")
        .AddPathSegment("c")
        .AddQueryString("attr1", "1")
        .AddQueryString("attr2", "2")
        .WithFragment("gohere")
        .Build();

    // To modify http://www.me.com/a/b/c?foo=bar#here to instead be https://www.me.com/a/b/d?attr1=1
    url = new UrlBuilder("http://www.me.com/a/b/c?foo=bar#here")
        .AsHttps()
        .RemoveLastSegment()
        .AddPathSegment("d")
        .ClearQueryString()
        .AddQueryString("attr1", "1")
        .WithFragment("")
        .Build();

    // Will generate the path: /a/b/c?attr1=1&attr2=2#gohere
    var path = new RelativePathBuilder()
        .AddPathSegment("a")
        .AddPathSegment("b")
        .AddPathSegment("c")
        .AddQueryString("attr1", "1")
        .AddQueryString("attr2", "2")
        .WithFragment("gohere")
        .Build();