Question

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
Was it helpful?

Solution

You're looking for the Uri class:

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top