简体   繁体   中英

Extract domain with subdomain in side class

I have an ASP.NET 3.5 Web application with C# 2008 .

What I need to do is, I want to extract full domain name in side a class method from Current URL.

For example :

I do have Current URL like :

http://subdomain.domain.com/pagename.aspx

OR

https://subdomain.domain.com/pagename.aspx?param=value&param2=value2

Then the result should be like,

http://subdomain.domain.com

OR

https://subdomain.domain.com

您正在寻找Uri类:

new Uri(str).GetLeftPart(UriPartial.Authority)

Create a Uri and query the Host property:

var uri = new Uri(str);
var host = uri.Host;

(Later)

I just realized that you want the scheme and the domain. In that case, @SLaks answer is the one you want. You could do it by combining the uri.Scheme and uri.Host , but that can get messy for things like mailto urls, etc.

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