Extract base URl from a string in c#?

前端 未结 5 2039
情书的邮戳
情书的邮戳 2021-02-03 21:31

I am currently working on a project with .NET 1.1 framework and I am stuck at this point. I have a string like \"http://www.example.com/mypage/default.aspx\" or it might be \"ht

5条回答
  •  借酒劲吻你
    2021-02-03 21:42

    Short Answer

    myUri.GetLeftPart(System.UriPartial.Authority)
    

    Long Answer
    Assuming "Base URI" means something like http://www.example.com, you can get the base uri like this:

    var myUri= new Uri("http://www.example.com/mypage/default.aspx");    
    var baseUri = myUri.GetLeftPart(System.UriPartial.Authority)
    

    This gives: http://www.example.com

    Note: uri.Host gives: www.example.com (not including port or scheme)

提交回复
热议问题