如何组合 URI

发布于 2024-07-21 05:49:11 字数 628 浏览 4 评论 0原文

我有两个 Uri 对象传递到一些代码中,一个是目录,另一个是文件名(或相对路径):

var a = new Uri("file:///C:/Some/Dirs");
var b = new Uri("some.file");

当我尝试像这样组合它们时,

var c = new Uri(a,b);

我得到

文件:///C:/Some/some.file

我希望获得与 Path.Combine 相同的效果(因为这是我需要的旧代码取代):

文件:///C:/Some/Dirs/some.file

我想不出一个干净的解决方案。

丑陋的解决方案是在 Uri 中添加 /(如果它不存在)

string s = a.OriginalString;
if(s[s.Length-1] != '/')
   a = new Uri(s + "/");

I have two Uri objects passed into some code, one is a directory and the other is a filename (or a relative path)

var a = new Uri("file:///C:/Some/Dirs");
var b = new Uri("some.file");

when I try and combine them like this:

var c = new Uri(a,b);

I get

file:///C:/Some/some.file

where I wold expect to get the same effect as with Path.Combine (as that is the old code I need to replace):

file:///C:/Some/Dirs/some.file

I can't think of a clean solution to this.

The ugly solution being to add a / to the Uri if it's not there

string s = a.OriginalString;
if(s[s.Length-1] != '/')
   a = new Uri(s + "/");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

七婞 2024-07-28 05:49:11

这应该对你有用:

var baseUri = new Uri("http://www.briankeating.net");
var absoluteUri = new Uri(baseUri, "/api/GetDefinitions");

这个 构造函数 遵循标准相对 URI 规则,因此 / 很重要:

  • http://example.net + foo = http://example.net/foo
  • http://example.net/foo/bar + baz = http://example. net/foo/baz
  • http://example.net/foo/ + bar = http://example.net/foo/bar
  • http://example.net/foo + bar = http://example.net/bar
  • http ://example.net/foo/bar/ + /baz = http://example.net/baz

This should do the trick for you:

var baseUri = new Uri("http://www.briankeating.net");
var absoluteUri = new Uri(baseUri, "/api/GetDefinitions");

This constructor follow the standard relative URI rules so the / are important :

  • http://example.net + foo = http://example.net/foo
  • http://example.net/foo/bar + baz = http://example.net/foo/baz
  • http://example.net/foo/ + bar = http://example.net/foo/bar
  • http://example.net/foo + bar = http://example.net/bar
  • http://example.net/foo/bar/ + /baz = http://example.net/baz
人心善变 2024-07-28 05:49:11

好吧,您必须以某种方式告诉 Uri 最后一部分是目录而不是文件。 对我来说,使用尾部斜杠似乎是最明显的方法。

请记住,对于许多 Uris 来说,您得到的答案是完全正确的。 例如,如果您的 Web 浏览器正在呈现

http://foo.com/bar/index.html

,并且它看到“other.html”的相对链接,则它会转到“

http://foo.com/bar/other.html

不”。

http://foo.com/bar/index.html/other.html

在“目录”Uris 上使用尾部斜杠是一种非常熟悉的方式,建议相对 Uris 应该只是附加而不是附加更换。

Well, you're going to have to tell the Uri somehow that the last part is a directory rather than a file. Using a trailing slash seems to be the most obvious way to me.

Bear in mind that for many Uris, the answer you've got is exactly right. For example, if your web browser is rendering

http://foo.com/bar/index.html

and it sees a relatively link of "other.html" it then goes to

http://foo.com/bar/other.html

not

http://foo.com/bar/index.html/other.html

Using a trailing slash on "directory" Uris is a pretty familiar way of suggesting that relative Uris should just append instead of replacing.

吻风 2024-07-28 05:49:11

你可以试试这个扩展方法! 永远有效! ;-)

 public static class StringExtension
    {
        public static string UriCombine(this string str, string param)
        {
            if (!str.EndsWith("/"))
            {
                str = str + "/";
            }
            var uri = new Uri(str);
            return new Uri(uri, param).AbsoluteUri;
        }
    }

安吉洛,亚历山德罗

You can try this extension method! Works always! ;-)

 public static class StringExtension
    {
        public static string UriCombine(this string str, string param)
        {
            if (!str.EndsWith("/"))
            {
                str = str + "/";
            }
            var uri = new Uri(str);
            return new Uri(uri, param).AbsoluteUri;
        }
    }

Angelo, Alessandro

五里雾 2024-07-28 05:49:11

在第一个 URI 的末尾添加斜杠,URI 将忽略多个斜杠 (/)

var a = new Uri("file:///C:/Some/Dirs/");

编辑:

var a = new Uri("file:///C:/Some/Dirs");
var b = new Uri("some.file",  UriKind.Relative);
var c = new Uri(Path.Combine(a.ToString(), b.ToString()));
MessageBox.Show(c.AbsoluteUri);

add a slash end of your first uri, URI will ignore more than one slash (/)

var a = new Uri("file:///C:/Some/Dirs/");

EDIT:

var a = new Uri("file:///C:/Some/Dirs");
var b = new Uri("some.file",  UriKind.Relative);
var c = new Uri(Path.Combine(a.ToString(), b.ToString()));
MessageBox.Show(c.AbsoluteUri);
桃气十足 2024-07-28 05:49:11

为什么不直接继承 Uri 并使用它,即。 在构造函数中需要做什么来修复它? 假设重构是在装配内部或在可及范围内的,那么重构是很便宜的。

Why not just inherit from Uri and use that, ie. do in constructor what you need to do to fix it up? Refactoring is cheap assuming this is internal to assembly or within reach..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文