简体   繁体   中英

c# help using system.uri functions

i have a STRING in C# with a long url such as: http://mysite.com/testing/testingPages/area/ten/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获取域或使用myURI对象执行任何myURI

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM