Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 86bd393

Browse files
committed
Add string+PathString operator to prevent too much string<->PathString implicit conversion.
1 parent 0737ea3 commit 86bd393

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Microsoft.AspNet.Http.Core/PathString.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ public override int GetHashCode()
215215
return !left.Equals(right, StringComparison.OrdinalIgnoreCase);
216216
}
217217

218+
/// <summary>
219+
/// </summary>
220+
/// <param name="left">The left parameter</param>
221+
/// <param name="right">The right parameter</param>
222+
/// <returns>The ToString combination of both values</returns>
223+
public static string operator +(string left, PathString right)
224+
{
225+
// This overload exists to prevent the implicit string<->PathString converter from
226+
// trying to call the PathString+PathString operator for things that are not path strings.
227+
return string.Concat(left, right.ToString());
228+
}
229+
218230
/// <summary>
219231
/// Operator call through to Add
220232
/// </summary>

test/Microsoft.AspNet.Http.Core.Tests/PathStringTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,19 @@ public void AddPathString_HandlesLeadingAndTrailingSlashes(string appString, str
5050
// Assert
5151
Assert.Equal(expected, result.Value);
5252
}
53+
54+
[Fact]
55+
public void ImplicitStringConverters_WorksWithAdd()
56+
{
57+
var scheme = "http";
58+
var host = new HostString("localhost:80");
59+
var pathBase = new PathString("/base");
60+
var path = new PathString("/path");
61+
var query = new QueryString("?query");
62+
var fragment = new FragmentString("#frag");
63+
64+
var result = scheme + "://" + host + pathBase + path + query + fragment;
65+
Assert.Equal("http://localhost:80/base/path?query#frag", result);
66+
}
5367
}
5468
}

0 commit comments

Comments
 (0)