我想在asp.net中检索我的域名网址.
例如,如果我的网址是:
http://www.mydomain.com/blog/currentPage.aspx?id=156
我只想要这个部分
http://www.mydomain.com/blog/
谁能帮我吗?
你有很多选择:
string root = this.ResolveUrl("~")
要么
Uri requestUri = Context.Request.Url; string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);
要么
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
如果你想将/ blog附加到最后两个,请添加
+ Request.ApplicationPath
// Request.Uri Uri originalUrl = new Uri("https://www.example.com/blog/currentPage.aspx?id=156"); // www.examle.com string domain = originalUrl.Host; // https://www.example.com string domainUrl = String.Concat(originalUrl.Scheme, Uri.SchemeDelimiter, originalUrl.Host);