Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b1d037a88 | ||
|
|
be92d91ef6 | ||
|
|
a1527c058c | ||
|
|
908d0e2433 | ||
|
|
e9f3e5ed8b | ||
|
|
8b053f0f77 |
57
Readme.md
57
Readme.md
@@ -1,10 +1,15 @@
|
||||
## UriBuilder.Fluent
|
||||
# UriBuilder.Fluent
|
||||
[](https://coveralls.io/github/TerribleDev/UriBuilder.Fluent) [](https://ci.appveyor.com/project/tparnell8/uribuilder-fluent/branch/master)
|
||||
|
||||
This places extension methods over System.UriBuilder to help deal with query string parameters, and create more of a fluent interface. Unlike other projects, this NetStandardLibrary compliant package builds ontop of trusty UriBuilder, does not use custom Uri generators, or have outside dependencies. Unit tests continue to be a first class citizen!
|
||||
|
||||
This lets you do things like
|
||||
## Getting started
|
||||
|
||||
Just install the nuget package `install-package UriBuilder.Fluent` and thats it. The extension methods should be available to you!
|
||||
|
||||
## Code Example
|
||||
|
||||
This lets you do things like
|
||||
|
||||
```csharp
|
||||
|
||||
@@ -16,8 +21,6 @@ new UriBuilder()
|
||||
.UseHttps()
|
||||
.ToString()
|
||||
|
||||
|
||||
|
||||
```
|
||||
result: `https://awesome.com/seg?awesome=yodawg&fun=cool,yay`
|
||||
|
||||
@@ -35,16 +38,44 @@ result: `https://awesome.com/yo?id=5`
|
||||
you can even pass a dictionary of parameters
|
||||
|
||||
```csharp
|
||||
var dictionary = new Dictionary<string, string>()
|
||||
{
|
||||
["yo"] = "dawg"
|
||||
};
|
||||
new UriBuilder("http://awesome.com")
|
||||
.WithParameter(dictionary);
|
||||
http://awesome.com/?yo=dawg
|
||||
|
||||
var dictionary = new Dictionary<string, string>()
|
||||
{
|
||||
["yo"] = "dawg"
|
||||
};
|
||||
|
||||
new UriBuilder("http://awesome.com")
|
||||
.WithParameter(dictionary);
|
||||
|
||||
```
|
||||
result: `http://awesome.com/?yo=dawg`
|
||||
|
||||
### ToEscapeString() vs. ToEscapeDataString()
|
||||
|
||||
There are two extension methods for performing Uri encoding.
|
||||
|
||||
#### ToEscapeString()
|
||||
Performs encoding only on the Uri query string and returns the Uri as a string.
|
||||
|
||||
```csharp
|
||||
|
||||
var uri = new UriBuilder("https://awesome.com")
|
||||
.WithParameter("yo", "dawg<")
|
||||
.ToEscapeString();
|
||||
|
||||
```
|
||||
|
||||
## Getting started
|
||||
result: `http://awesome.com/?yo=dawg%3C`
|
||||
|
||||
Just install the nuget package `install-package UriBuilder.Fluent` and thats it. The extension methods should be available to you!
|
||||
#### ToEscapeDataString()
|
||||
Performs encoding on the whole Uri and returns the Uri as a string.
|
||||
|
||||
```csharp
|
||||
|
||||
var uri = new UriBuilder("https://awesome.com")
|
||||
.WithParameter("yo", "dawg<")
|
||||
.ToEscapeDataString();
|
||||
|
||||
```
|
||||
|
||||
result: `http%3A%2F%2Fawesome.com%2F%3Fyo%3Ddawg%3C`
|
||||
@@ -36,6 +36,17 @@ namespace FluentUriBuilder.Tests
|
||||
Assert.Equal("http://awesome.com/?awesome=yodawg", url.Uri.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PathAndQuery()
|
||||
{
|
||||
var url = new UriBuilder().WithPathSegment("/awesome/v1/").WithParameter("awesome", "cool").PathAndQuery();
|
||||
Assert.Equal("/awesome/v1/?awesome=cool", url);
|
||||
url = new UriBuilder().WithPathSegment("/awesome/v1").WithParameter("awesome", "cool").PathAndQuery();
|
||||
Assert.Equal("/awesome/v1?awesome=cool", url);
|
||||
url = new UriBuilder().WithPathSegment("/awesome/v1").PathAndQuery();
|
||||
Assert.Equal("/awesome/v1", url);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestAddParameterArray()
|
||||
{
|
||||
@@ -130,5 +141,21 @@ namespace FluentUriBuilder.Tests
|
||||
.WithParameter(dictionary);
|
||||
Assert.Equal("http://awesome.com/?yo=dawg&troll=toll&hammer", url.Uri.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestToEscapedString()
|
||||
{
|
||||
var url = new UriBuilder("http://awesome.com")
|
||||
.WithParameter("yo","dawg<");
|
||||
Assert.Equal("http://awesome.com/?yo=dawg%3C", url.ToEscapeString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestToEscapedDataString()
|
||||
{
|
||||
var url = new UriBuilder("http://awesome.com")
|
||||
.WithParameter("yo","dawg<");
|
||||
Assert.Equal("http%3A%2F%2Fawesome.com%2F%3Fyo%3Ddawg%3C", url.ToEscapeDataString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"net461": {},
|
||||
"net461":{},
|
||||
"netcoreapp1.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.App": {
|
||||
|
||||
@@ -22,6 +22,7 @@ namespace System
|
||||
/// </summary>
|
||||
/// <param name="bld"></param>
|
||||
/// <param name="parameterDictionary"></param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
/// <returns></returns>
|
||||
public static UriBuilder WithParameter(this UriBuilder bld, IDictionary<string, string> parameterDictionary)
|
||||
{
|
||||
@@ -126,6 +127,8 @@ namespace System
|
||||
return bld;
|
||||
}
|
||||
|
||||
public static string PathAndQuery(this UriBuilder bld) => (bld.Path + bld.Query);
|
||||
|
||||
/// <summary>
|
||||
/// Use Https?
|
||||
/// </summary>
|
||||
@@ -137,5 +140,20 @@ namespace System
|
||||
bld.Scheme = predicate ? "https" : "http";
|
||||
return bld;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Escape Url query string
|
||||
/// </summary>
|
||||
/// <param name="bld"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToEscapeString(this UriBuilder bld) => Uri.EscapeUriString(bld.Uri.ToString());
|
||||
|
||||
/// <summary>
|
||||
/// Escape the whole Url string
|
||||
/// </summary>
|
||||
/// <param name="bld"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToEscapeDataString(this UriBuilder bld) => Uri.EscapeDataString(bld.Uri.ToString());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,15 +14,19 @@
|
||||
"extension"
|
||||
]
|
||||
},
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.0"
|
||||
},
|
||||
"authors": [
|
||||
"Tommy Parnell"
|
||||
],
|
||||
"frameworks": {
|
||||
"netstandard1.1": {
|
||||
"imports": "dnxcore50",
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.0"
|
||||
}
|
||||
},
|
||||
"net40": {
|
||||
"imports": "dnxcore50"
|
||||
}
|
||||
},
|
||||
"net45":{}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user