Getting a domain name of a site

A

Adam Tibi

Hi,

Is there a way of getting the domain name of tghe currently executing site,
I tried using this silly code:

public static string GetDomainName() {

string serverName =
System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToLower();

if(!serverName.Contains(".")) {

return serverName;

}

if(serverName.EndsWith(".com") || serverName.EndsWith(".net")) {

return serverName.Substring(serverName.LastIndexOf(".",
serverName.Length - ".com".Length - 1) + 1);

}

if(serverName.EndsWith(".co.uk")) {

return serverName.Substring(serverName.LastIndexOf(".",
serverName.Length - ".co.uk".Length - 1) + 1);

}

return null;

}

But I don't think that this is the right solution, does any one have a NEAT
solution for this, for example by using the URI class or some thing?

Adam Tibi
 
K

Karl Seguin [MVP]

Look at the Request.Url class, it has a number of useful properties.

Karl
 
A

Adam Tibi

Thank You Karl, actually, I am interested in getting the last portion of the
domain only, so if my site is www.xyz.co.uk then I need to get xyz.co.uk and
if my site is abc.xyz.com then I am only interested in the xyz.com part.

I still need to go through the logic of the method I provided below!

Ideas?
Regards,
Adam Tibi

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
Look at the Request.Url class, it has a number of useful properties.

Karl
--
http://www.openmymind.net/



Adam Tibi said:
Hi,

Is there a way of getting the domain name of tghe currently executing
site, I tried using this silly code:

public static string GetDomainName() {

string serverName =
System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToLower();

if(!serverName.Contains(".")) {

return serverName;

}

if(serverName.EndsWith(".com") || serverName.EndsWith(".net")) {

return serverName.Substring(serverName.LastIndexOf(".",
serverName.Length - ".com".Length - 1) + 1);

}

if(serverName.EndsWith(".co.uk")) {

return serverName.Substring(serverName.LastIndexOf(".",
serverName.Length - ".co.uk".Length - 1) + 1);

}

return null;

}

But I don't think that this is the right solution, does any one have a
NEAT solution for this, for example by using the URI class or some thing?

Adam Tibi
 
K

Karl Seguin [MVP]

if (address.StartsWith("www."))
{
address = address.SubString(4);
}

Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/


Adam Tibi said:
Thank You Karl, actually, I am interested in getting the last portion of
the domain only, so if my site is www.xyz.co.uk then I need to get
xyz.co.uk and if my site is abc.xyz.com then I am only interested in the
xyz.com part.

I still need to go through the logic of the method I provided below!

Ideas?
Regards,
Adam Tibi

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
Look at the Request.Url class, it has a number of useful properties.

Karl
--
http://www.openmymind.net/



Adam Tibi said:
Hi,

Is there a way of getting the domain name of tghe currently executing
site, I tried using this silly code:

public static string GetDomainName() {

string serverName =
System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToLower();

if(!serverName.Contains(".")) {

return serverName;

}

if(serverName.EndsWith(".com") || serverName.EndsWith(".net")) {

return serverName.Substring(serverName.LastIndexOf(".",
serverName.Length - ".com".Length - 1) + 1);

}

if(serverName.EndsWith(".co.uk")) {

return serverName.Substring(serverName.LastIndexOf(".",
serverName.Length - ".co.uk".Length - 1) + 1);

}

return null;

}

But I don't think that this is the right solution, does any one have a
NEAT solution for this, for example by using the URI class or some
thing?

Adam Tibi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top