Dev
Dev

Reputation: 6710

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

Upvotes: 0

Views: 544

Answers (2)

Jim Mischel
Jim Mischel

Reputation: 133995

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.

Upvotes: 2

SLaks
SLaks

Reputation: 887453

You're looking for the Uri class:

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

Upvotes: 5

Related Questions