UriBuilderExtended 1.2.1

Extensions to .NET's System.UriBuilder.

Install-Package UriBuilderExtended -Version 1.2.1
dotnet add package UriBuilderExtended --version 1.2.1
<PackageReference Include="UriBuilderExtended" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UriBuilderExtended --version 1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

UriBuilderExtended

Extensions to .NET's System.UriBuilder.

Maintainers wanted!

  • Would you like to maintain UriBuilderExtended?
  • Alternatively include it in your own library project where it makes sense?
  • Are you from microsoft and wants to put this code into UriBuilder?

If you can answer yes to any of these questions, then you are most welcome!
I have shifted path and do not work with C# and .NET anymore. Therefore I would like to hand over this project to someone who can keep the code alive.

Introduction

A thing that is missing from .NET's System.UriBuilder is a good way to edit the query string. That is what made me look for others solution to this problem, and ultimately made me start this project.

Other good solutions exist that may cover your needs, but I was unable to find one that that covered my requirements:

  1. Do anything UriBuilder does
  2. Set the query string with support for multiple values
  3. Be as simple as possible

I have built this with great inspiration from URI.js, which is a very good library for modifying URIs in JavaScript.

Documentation

Install the package from NuGet

Add using UriBuilderExtended; to your source files, and you will have the following methods available on your UriBuilder objects:

bool HasQuery(string key)
Check for the existence of a query with the given key

bool HasQuery(string key, params string[] values)
Check for the existence of a query with the given key and the given values

UriBuilder RemoveQuery(string key)
Remove any query with the given key

UriBuilder SetQuery(string key, params string[] values)
Sets the query parameter for a given key

UriBuilder AddQuery(string key, params string[] values)
Adds a query parameter for a given key

List&lt;string&gt; GetQueryValues(string key)
Get all values for the given key

Examples

    UriBuilder builder = new UriBuilder("http://www.mysite.net/");
    // URL is now http://www.mysite.net/
    
    // URL string  is obtained from builder.Uri.ToString(), not builder.ToString()
    // as it sometimes renders differently to what you'd expect

    builder.AddQuery("myKey", "myValue1");
    // URL is now http://www.mysite.net/?myKey=myValue1

    builder.AddQuery("myOtherKey", "myOtherValue1");
    // URL is now http://www.mysite.net/?myKey=myValue1&myOtherKey=myOtherValue1

    builder.AddQuery("myKey", "myValue2", "myValue3");
    // URL is now http://www.mysite.net/?myKey=myValue1&myKey=myValue2&myKey=myValue3&myOtherKey=myOtherValue1

    builder.SetQuery("myKey", "newValue1", "newValue2");
    // URL is now http://www.mysite.net/?myOtherKey=myOtherValue1&myKey=newValue1&myKey=newValue2

    builder.HasQuery("myKey");
    builder.HasQuery("myOtherKey");
    // true

    builder.HasQuery("myKey", "myValue1");
    // false

    builder.HasQuery("myKey", "newValue1");
    // true

    builder.HasQuery("notMymyKey");
    // false

    builder.GetQueryValues("myKey");
    // An ICollection containing values "newValue1" and "newValue2"

UriBuilderExtended

Extensions to .NET's System.UriBuilder.

Maintainers wanted!

  • Would you like to maintain UriBuilderExtended?
  • Alternatively include it in your own library project where it makes sense?
  • Are you from microsoft and wants to put this code into UriBuilder?

If you can answer yes to any of these questions, then you are most welcome!
I have shifted path and do not work with C# and .NET anymore. Therefore I would like to hand over this project to someone who can keep the code alive.

Introduction

A thing that is missing from .NET's System.UriBuilder is a good way to edit the query string. That is what made me look for others solution to this problem, and ultimately made me start this project.

Other good solutions exist that may cover your needs, but I was unable to find one that that covered my requirements:

  1. Do anything UriBuilder does
  2. Set the query string with support for multiple values
  3. Be as simple as possible

I have built this with great inspiration from URI.js, which is a very good library for modifying URIs in JavaScript.

Documentation

Install the package from NuGet

Add using UriBuilderExtended; to your source files, and you will have the following methods available on your UriBuilder objects:

bool HasQuery(string key)
Check for the existence of a query with the given key

bool HasQuery(string key, params string[] values)
Check for the existence of a query with the given key and the given values

UriBuilder RemoveQuery(string key)
Remove any query with the given key

UriBuilder SetQuery(string key, params string[] values)
Sets the query parameter for a given key

UriBuilder AddQuery(string key, params string[] values)
Adds a query parameter for a given key

List&lt;string&gt; GetQueryValues(string key)
Get all values for the given key

Examples

    UriBuilder builder = new UriBuilder("http://www.mysite.net/");
    // URL is now http://www.mysite.net/
    
    // URL string  is obtained from builder.Uri.ToString(), not builder.ToString()
    // as it sometimes renders differently to what you'd expect

    builder.AddQuery("myKey", "myValue1");
    // URL is now http://www.mysite.net/?myKey=myValue1

    builder.AddQuery("myOtherKey", "myOtherValue1");
    // URL is now http://www.mysite.net/?myKey=myValue1&myOtherKey=myOtherValue1

    builder.AddQuery("myKey", "myValue2", "myValue3");
    // URL is now http://www.mysite.net/?myKey=myValue1&myKey=myValue2&myKey=myValue3&myOtherKey=myOtherValue1

    builder.SetQuery("myKey", "newValue1", "newValue2");
    // URL is now http://www.mysite.net/?myOtherKey=myOtherValue1&myKey=newValue1&myKey=newValue2

    builder.HasQuery("myKey");
    builder.HasQuery("myOtherKey");
    // true

    builder.HasQuery("myKey", "myValue1");
    // false

    builder.HasQuery("myKey", "newValue1");
    // true

    builder.HasQuery("notMymyKey");
    // false

    builder.GetQueryValues("myKey");
    // An ICollection containing values "newValue1" and "newValue2"

Release Notes

1.2.1
     Added GetQueryKeys method.
     
     1.2.0
     Added GetQueryValues method.
     
     1.1.0
     First stable release. Changed version scheme.

Dependencies

This package has no dependencies.

Version History

Version Downloads Last updated
1.2.1 7,991 6/1/2016
1.2.0 293 6/1/2016
1.1.0 268 6/1/2016