I was recently looking at this SO thread: Path.Combine for URLs? and thought that maybe I could create something like Path.Combine. So I wrote:
private string combineUrlParts(params string[] urlParts)
{
var myUrl = new Uri(urlParts[0]);
for (int x = 1; x < urlParts.Length; x++)
{
if (!Uri.TryCreate(myUrl, urlParts[x], out myUrl))
{
// Log failure
}
}
return myUrl.ToString();
}
The idea being to list a baseUrl ("http://someurl.com/"), a path ("/company/5/"), and another part ("/Financials/index.aspx") and have it all magically combined.
This method almost works. The first time thru the loop, it combines the base Url and the first path just fine. But the 2nd time thru the loop, Uri.TryCreate just overwrites the Path with the second part yielding:
http://someurl.com/Financials/index.aspx
instead of what I was expecting:
http://someurl.com/company/5/Financials/index.aspx
Any ideas what's going on here?