c# help using system.uri functions
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);
精彩评论