determining the web domain name

  • Thread starter Thread starter Jonny Wilkinson
  • Start date Start date
J

Jonny Wilkinson

Hi

I need to be able to determine the domain name the web page has been called
from. I have a site that can be accessed through different domain names
(they all point to the same IP address) eg. www.abc.com and www.bcd.com

Is there a way to do this in the ASPX code in C#

Many thanks

Jonny

P.S I did post this message before but it didn't show up in my news reader.
 
string[] url = Request.Url.ToString().Replace(@"http://",
String.Empty).Split('/');

Then examine url[0].

string currentDomain = url[0];

You can even do logic like:

switch(currentDomain)
{
case "www.mysite1.com":
//do some work here
break;
default:
//main site, catch all
//Do work here
break;
}

---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Now that I think for a minute (getting so used to parsing), the System.Uri
object makes things even simpler. Try:

System.Uri url = Request.Url;
string hostname = url.Host.ToString();

In VB.NET:

Dim url As System.Uri = Request.Url
Dim hostname As String = url.Host.ToString()


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Fantastic - thanks Gregory!



Cowboy (Gregory A. Beamer) - MVP said:
Now that I think for a minute (getting so used to parsing), the System.Uri
object makes things even simpler. Try:

System.Uri url = Request.Url;
string hostname = url.Host.ToString();

In VB.NET:

Dim url As System.Uri = Request.Url
Dim hostname As String = url.Host.ToString()


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 

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

Back
Top