C# 结合 URI 和路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/679171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:08:58  来源:igfitidea点击:

Combining URIs and Paths

c#.netweb-services

提问by Richard Slater

I am retro-fitting an application to make use of a PHP HTTP proxy (for caching) instead of the actual API server, the application currently combines the server URI and the path with the code:

我正在改造一个应用程序以使用 PHP HTTP 代理(用于缓存)而不是实际的 API 服务器,该应用程序目前将服务器 URI 和路径与代码结合起来:

methodUri = new Uri(apiUri, method.Path)

Where:

在哪里:

The result of the above statement is

上述语句的结果是

"http://api.eve-online.com/char/SkillIntraining.xml.aspx" (System.Uri Object)

To use the PHP HTTP proxy the request would have to be changed as follows

要使用 PHP HTTP 代理,必须按如下方式更改请求

The output I was expecting was:

我期待的输出是:

"http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx" (System.Uri Object)

However the output I get is:

但是我得到的输出是:

"http://www.r-s.co.uk/char/SkillIntraining.xml.aspx" (System.Uri Object)

I understand that this is the correct functionality of the constructor Uri(Uri, string), my question is what would be a better function or constructor to use in its place to get the output I expect? I have tried removing the leading "/" in method.Path taking it from an absolute path to a relative path however that did not help.

我知道这是构造函数 Uri(Uri, string) 的正确功能,我的问题是什么是更好的函数或构造函数来代替它来获得我期望的输出?我尝试删除 method.Path 中的前导“/”,将其从绝对路径转换为相对路径,但这并没有帮助。

NOTE:both solutions below do work, however System.UriBuilder provides a more robust mechanism for combining URI's and paths and in my case resulted in fewer changes to resources than using System.Uri. Had I the choice I would mark both answers as correct.

注意:下面的两种解决方案都有效,但是 System.UriBuilder 提供了一种更强大的机制来组合 URI 和路径,在我的情况下,与使用 System.Uri 相比,对资源的更改更少。如果我有选择,我会将两个答案都标记为正确。

采纳答案by blowdart

Don't use the Uri object, use a UriBuilder- it copes way better with missing slashes

不要使用 Uri 对象,使用UriBuilder- 它可以更好地处理丢失的斜线

So

所以

Uri apiUri = new Uri("http://www.r-s.co.uk/eproxy.php");
string methodPath = "/char/SkillIntraining.xml.aspx";

System.UriBuilder uriBuilder = new System.UriBuilder(apiUri);
uriBuilder.Path += methodPath;

Console.WriteLine(uriBuilder.Uri.ToString());

works as expected and produces http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx

按预期工作并产生http://www.rs.co.uk/eproxy.php/char/SkillIntraining.xml.aspx

回答by Jim Mischel

Add a trailing "/" to apiUri, and remove the leading "/" from method.Path:

向 apiUri 添加尾随的“/”,并从 method.Path 中删除前导的“/”:

Uri apiUri = new Uri("http://www.r-s.co.uk/eproxy.php/");
string path = "char/SkillIntraining.xml.aspx";
Uri uri = new Uri(apiUri, path);
Console.WriteLine(uri.ToString());

Will print:

将打印:

http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx