[英]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 我在C#中有一个带有长URL的STRING,例如: http : //mysite.com/testing/testingPages/area/ten/p1.aspx
how can i use system.uri class to get the http://mysite.com part only ? 我怎样才能使用system.uri类来获取http://mysite.com部分?
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: 我相信Uri.GetLeftPart
就是你所追求的:
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类上调用GetLeftPart方法 ,如下所示:
Uri uri = new Uri("http://mysite.com/testing/testingPages/area/ten/p1.aspx");
string baseUri = uri.GetLeftPart(UriPartial.Authority);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.