Path.Combine For Urls

Path.Combine for URLs?

There is a Todd Menier's comment above that Flurl includes a Url.Combine.

More details:

Url.Combine is basically a Path.Combine for URLs, ensuring one
and only one separator character between parts:


var url = Url.Combine(
"http://MyUrl.com/",
"/too/", "/many/", "/slashes/",
"too", "few?",
"x=1", "y=2"
// result: "http://www.MyUrl.com/too/many/slashes/too/few?x=1&y=2"

Get Flurl.Http on NuGet:

PM> Install-Package Flurl.Http

Or get the stand-alone URL builder without the HTTP features:

PM> Install-Package Flurl

Path.Combine for URLs?

There is a Todd Menier's comment above that Flurl includes a Url.Combine.

More details:

Url.Combine is basically a Path.Combine for URLs, ensuring one
and only one separator character between parts:


var url = Url.Combine(
"http://MyUrl.com/",
"/too/", "/many/", "/slashes/",
"too", "few?",
"x=1", "y=2"
// result: "http://www.MyUrl.com/too/many/slashes/too/few?x=1&y=2"

Get Flurl.Http on NuGet:

PM> Install-Package Flurl.Http

Or get the stand-alone URL builder without the HTTP features:

PM> Install-Package Flurl

Path.Combine for URLs (part 2)

Flurl [disclosure: I'm the author] is a tiny URL builder library that can fill the gap with its Url.Combine method:

string url = Url.Combine("http://www.foo.com/", "/too/", "/many/", "/slashes/", "too", "few");
// result: "http://www.foo.com/too/many/slashes/too/few"

You can get it via NuGet: Install-Package Flurl.

I also wanted to point out that you can dramatically improve the efficiency of your code by downloading the files in parallel. There's a couple ways to do that. If you're on .NET 4.5 or above and can rewrite downloadFile as an async method, then your best option would be to replace your for loop with something like this:

var tasks = filenames.Select(f => downloadFileAsync(Url.Combine(folder, f)));
await Task.WhenAll(tasks);

Otherwise, if you're stuck on .NET 4, you can still achieve parallelism easily by using Parallel.ForEach:

Parallel.ForEach(filenames, f => downloadFile(Url.Combine(folder, f)));

Path.Combine on urls gives exception : The given path's format is not supported

Path.Combine does not support urls.

You will have to translate the url to a (relative) file path first if you want to use Path.Combine

If you want to manipulate urls you can use the Url constructor that takes a base url and a relative url and combines them.

Combine URL paths with path.Join()

The function path.Join expects a path, not a URL. Parse the URL to get a path and join with that path:

u, err := url.Parse("http://foo")
if err != nil { log.Fatal(err) }
u.Path = path.Join(u.Path, "bar.html")
s := u.String()
fmt.Println(s) // prints http://foo/bar.html

Use the url.JoinPath function in Go 1.19 or later:

s, err := url.JoinPath("http://foo", "bar.html")
if err != nil { log.Fatal(err) }
fmt.Println(s) // prints http://foo/bar.html

Use ResolveReference if you are resolving a URI reference from a base URL. This operation is different from a simple path join: an absolute path in the reference replaces the entire base path; the base path is trimmed back to the last slash before the join operation.

base, err := url.Parse("http://foo/quux.html")
if err != nil {
log.Fatal(err)
}
ref, err := url.Parse("bar.html")
if err != nil {
log.Fatal(err)
}
u := base.ResolveReference(ref)
fmt.Println(u.String()) // prints http://foo/bar.html

Notice how quux.html in the base URL does not appear in the resolved URL.

\ in path.combine in c#

It is because Path.Combine is meant to combine typical directory path, something like:

C:\MyDir\MyDir2\MyMyDir

where the separator is \, not URL where the separator is /:

http://stackoverflow.com/questions/37249357/in-path-combine-in-c-sharp/37249373#37249373

If you want to combine URL path, you could use Uri instead:

Uri baseUri = new Uri(mypath);
Uri myUri = new Uri(baseUri, "Upload/Images/");

Path Combine of Url in Html


<img src='<%# new Uri(new Uri(Path), Eval("Image")).AbsoluteUri %>' />

C#: Path.Combine not giving full path

The second argument to Path.Combine ("/ABC/XYZ.exe") starts with a slash, which is putting you back at the root. Remove this leading slash and you should get the output you are after.

Path.Combine(Environment.GetEnvironmentVariable("PROGRAMFILES(X86)"), "ABC/XYZ.exe")

From the documentation:

If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned.




Related Topics



Leave a reply



Submit