开发者

c# help using system.uri functions

开发者 https://www.devze.com 2022-12-28 19:44 出处:网络
i have a STRING in C# with a long url such as: http://mysite.com/testing/testingPages/area/t开发者_StackOverflowen/p1.aspx

i have a STRING in C# with a long url such as: http://mysite.com/testing/testingPages/area/t开发者_StackOverflowen/p1.aspx

how can i use system.uri class to get the http://mysite.com part only ?


Uri myURI = new Uri("http://mysite.com/testing/testingPages/area/ten/p1.aspx");

myURI.Host gets the domain or do whatever you want with the myURI object


I believe Uri.GetLeftPart is what you're after:

using System;

public class Test
{
    static void Main()
    {
        string text = "http://mysite.com/testing/testingPages/area/ten/p1.aspx";
        Uri uri = new Uri(text);
        // Prints http://mysite.com
        Console.WriteLine(uri.GetLeftPart(UriPartial.Authority));
    }
}


If you want to specifically get the part up to the domain (including scheme, username, password, and port), then you would call the GetLeftPart method on the Uri class like so:

Uri uri = new Uri("http://mysite.com/testing/testingPages/area/ten/p1.aspx");
string baseUri = uri.GetLeftPart(UriPartial.Authority);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号