Path.Combine针对URL(第2部分) [英] Path.Combine for URLs (part 2)

查看:188
本文介绍了Path.Combine针对URL(第2部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关一段时间,现在,我一直在寻找上的URL工作的Path.Combine方法。这是类同 Path.Combine的网址吗?一个很大的区别的。

For awhile now, I've been searching for a Path.Combine method that works on URLs. This is similiar to Path.Combine for Urls? with one big difference.

我会用一个例子来说明。假设我们有一个基本的URL: http://example.com/somefolder 和一个文件: foo.txt的。因此,全路径将是: http://example.com/somefolder/foo.txt 。听起来很简单,不是吗?哈

I'll illustrate with an example. Say we have a base url: http://example.com/somefolder and a file: foo.txt. Thus, the full path would be: http://example.com/somefolder/foo.txt. Sounds simple, right? Ha.

我试过Uri类: Uri.TryCreate(新的URI(http://example.com/somefolder), foo.txt的,出X); 这就造成了。http://example.com/foo.txt

I tried the Uri class: Uri.TryCreate(new Uri("http://example.com/somefolder"), "foo.txt", out x); which resulted in "http://example.com/foo.txt".

然后我试图路径: System.IO.Path.Combine(http://example.com/somefolder,foo.txt的); 这就造成了http://example.com/somefolder\foo.txt ......近了,但仍然没有。

Then I tried Path: System.IO.Path.Combine("http://example.com/somefolder", "foo.txt"); which resulted in "http://example.com/somefolder\foo.txt"... Closer, but still no.

有关几脚,我又试图: System.IO.Path.Combine(http://example.com/somefolder/,foo.txt的)这就造成了http://example.com/somefolder/foo.txt

For kicks, I then tried: System.IO.Path.Combine("http://example.com/somefolder/", "foo.txt") which resulted in "http://example.com/somefolder/foo.txt".

最后一个工作,但它基本上做字符串连接在这一点

The last one worked, but it's basically doing string concatenation at that point.

所以,我认为我有两个选择:

So I think I have two options:


  • 使用Path.Combine和替换所有\与/

  • 使用基本字符串连接

我失去了一个内置在这一框架方法

Am I missing a built in framework method for this?

更新:用法情况下,我已经是下载一堆文件。我的代码看起来是这样的:

UPDATE: The usage case I have is for downloading a bunch of files. My code looks like this:

    public void Download()
    {
        var folder = "http://example.com/somefolder";
        var filenames = getFileNames(folder);

        foreach (var name in filenames)
        {
            downloadFile(new Uri(folder + "/" + name));
        }
    }



我恼火在不必使用字符串连接在开放的构造,以及不必检查是否需要斜杠(我在代码省略)。

I'm miffed at having to use string concat in the Uri constructor, as well having to check if the slash is needed (which I omitted in the code).

在我看来,这就是我想要做会来涨了不少,因为Uri类处理大量的其他协议,除了HTTP

It seems to me that what I'm trying to do would come up a lot, since the Uri class handles a lot of other protocols besides http.

推荐答案

Flurl [披露:我是作者]是一个很小的网址构建库,可以填补缺口的 Url.Combine 方法:

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"

您可以通过的NuGet得到它:安装封装Flurl

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

我也要指出的是,您可以极大地通过并联下载文件提高代码的效率。有几种方法可以做到这一点。如果你对上面的.NET 4.5或并且可以重写 downloadFile 作为一个异步方法,那么你最好的选择是,以取代循环使用是这样的:

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);



否则,如果你被困在.NET 4中,您仍然可以通过实现并行容易< A HREF =http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.foreach.aspx> Parallel.ForEach :

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

这篇关于Path.Combine针对URL(第2部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆