Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过编程更改URL的一部分以适应特定目的_C#_.net_String_Url - Fatal编程技术网

C# 通过编程更改URL的一部分以适应特定目的

C# 通过编程更改URL的一部分以适应特定目的,c#,.net,string,url,C#,.net,String,Url,我有网址https://test-wonder.test.local/attributes/testdata存储为 string url="https://test-wonder.test.local/attributes/testdata"; 由于可访问性问题,我需要将此url修改为以使用它。我必须以https://test-wonder.wondercloud.com/attributes/testdata 从我到目前为止所尝试的string.Replace()在一定程度上确实有效,但唯一

我有网址
https://test-wonder.test.local/attributes/testdata
存储为

string url="https://test-wonder.test.local/attributes/testdata";
由于可访问性问题,我需要将此url修改为以使用它。我必须以
https://test-wonder.wondercloud.com/attributes/testdata

从我到目前为止所尝试的
string.Replace()
在一定程度上确实有效,但唯一的方法是使用
string.Replace()
还是有更好的方法


也可以分解此URL的部分内容。例如,如果我想分解部分“
testdata
”或“
attributes
”,是否有办法这样做,并可能将其存储为某个变量。

为了动态更改主机名,可以使用字符串插值,在C#6及更高版本上可以将其写入:

string hostname = GetHostnameValue(); // replace this with the method to get your hostname value. For example, from a config file.
string url = $"https://{hostname}/attributes/testdata";
要分解URL,请查看Uri类。例如:

Uri u = new Uri(url);
var segments = u.Segments;

您可以使用:stringurl=string.Format(“https://{0}/attributes/testdata”文件夹);正是我需要的谢谢!