3

Coming from this question, i'm looking for a way to cascade a url like Path.Combine for file system does including a path-parameter.

My input are the following 3 parameters:

string host = "test.com"; //also possilbe: "test.com/"
string path = "/foo/"; //also possilbe: "foo", "/", "","/foo","foo/"
string file = "test.temp"; //also possilbe: "/test.temp"

The expected result is

http://test.com/foo/test.temp

This approach is the best I could find but it does'n work for all cases:

Uri myUri = new Uri(new Uri("http://" + host +"/"+ path), file);
Community
  • 1
  • 1
fubo
  • 44,811
  • 17
  • 103
  • 137
  • Why don't you trim host, path, file every time before you combine them. Then when you combine them use the forward slashes. – blogbydev Sep 30 '15 at 09:09
  • @singsuyash because `path` can be empty string – fubo Sep 30 '15 at 09:10
  • then trim based on case if forward slash is present, add forward slash if not empty and create a final host, path, file. Then combine them like scheme+host+path. – blogbydev Sep 30 '15 at 09:13
  • @fubo what about that extension method I used? – Reza Aghaei Sep 30 '15 at 12:05
  • @RezaAghaei yes that's pretty good, i already upvoted Ales Potocnik Hahonina's answer before i asked this question but i'm looking more for a slim one liner – fubo Sep 30 '15 at 12:14
  • @fubo I think the idea of using the extension method for this task is very friendly and I think it is one of the best answers of that tread that it worth to be mentioned here too:) – Reza Aghaei Sep 30 '15 at 12:20
  • @RezaAghaei i'm a friend of extension methods too but when you have too much of them and use them only once in your whole code, i better try to solve the problem without a extension instead of havin a overcrowded code – fubo Sep 30 '15 at 12:23

3 Answers3

1

You can use the UriBuilder class + IO.Path.Combine for the Path:

var builder = new UriBuilder();
builder.Host = host.Trim('/');
builder.Path = Path.Combine(path.Trim('/'), file.Trim('/'));
string result = builder.ToString();  // "http://test.com/foo/test.temp"

If you want the Uri-inctance just use the Uri-property of the UriBuilder.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

You could try using Uri.TryCreate():

Uri uri;
bool success = Uri.TryCreate(new Uri("http://" + host), path.Trim('/') + "/" + file.Trim('/'), out uri);

This will return false if the url is somehow in an incorrect format. However, if you are sure the format is correct, you can just use the Uri constructor:

var uri = new Uri(new Uri("http://" + host), path.Trim('/') + "/" + file.Trim('/'));
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
0

here you go

string url = string.Join("/", new[] { host, path, file }.Select(x => x.Trim('/'))
.Where(x => !String.IsNullOrEmpty(x)));
Byyo
  • 2,163
  • 4
  • 21
  • 35
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – ryanyuyu Sep 30 '15 at 12:56