以编程方式更改 URL 的一部分以适应特定目的



我将URLhttps://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;

最新更新