从包含方案的uri获取主机

时间:2015-11-20 09:36:37

标签: c# uri url-scheme

我目前的问题是,如果我有一个类似" onenote的网址:https://somerandomhost.com/foo1/foo2"主持人我的Uri对象的权威总是""

我目前的解决方案是:

var uri = new Uri(hyperlinkURL);
if(uri.Scheme != "https" && uri.Scheme != "http")
{
    uri = new Uri(uri.LocalPath);
}

有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

您的解决方案应该有效:

    var link = "onenote:https://somerandomhost.com/foo1/foo2"; 
    var uri = new Uri(link);
    if(uri.Scheme != "https" && uri.Scheme != "http")
    {
        uri = new Uri(uri.LocalPath);
    }
    Console.WriteLine(uri.Host);
    Console.WriteLine(uri.Authority);

https://dotnetfiddle.net/nLqXE4