How to determine IIS Application name from asp.net?

  • Thread starter Thread starter 1388-2/HB
  • Start date Start date
1

1388-2/HB

Title says it all.

Long version is that I have a win 2003 server box with 2 nics in it, 1.1.1.1
and 2.2.2.2, both hosting websites that reference the same code location.

"happywebsite.com" is hosted on 1.1.1.1 on a website object called "happy"
and "sadwebsite.com" is hosted on 2.2.2.2 on a website object called "sad".
The home directory for both website objects "happy" and "sad" is
"C:\Inetpub\wwwroot\website". From the user's perspective, there are 2
distinct, unrelated websites. From my perspective, there is 1 website to
code and maintain, with 2 different skins.

I do this because "happywebsite.com" and "sadwebsite.com" require identical
functionality and only appear different via skin; however, each requires its
own SSL certificate matching the respective url; so AFAIK, while they can
reference the same code, they must also be hosted on seperate website
objects in IIS in order to assign them their own SSL cert.

So in the code, if Request.URL.Host contains "happy" or "sad", I use the
corresponding skin and the website takes on the appropriate visual identity
(and some other changes such as the company name, etc.) depending upon which
url the user visited.

This all works great until the user reaches the code somehow omitting the
"happy" or "sad" urls (i.e., they type either IP directly, or some
antiquated DNS entry erroneously resolves "fubar.com" to 1.1.1.1 or
2.2.2.2). It's rare, but technically I cannot rely on Request.URL.Host to
know which website I'm supposed to be "posing" as 100% of the time.

I could probably hard-code IPs 1.1.1.1 and 2.2.2.2 into code as a "plan B"
decision maker in the event the Host is unrecognized, but I would rather be
able to ask IIS which application or website object I am running because the
"happy" website object is always going to use the happy skin irrespective of
host name or IP address and seems to be the ultimate definitive check.
 
Title says it all.

Long version is that I have a win 2003 server box with 2 nics in it, 1.1.1.1
and 2.2.2.2, both hosting websites that reference the same code location.

"happywebsite.com" is hosted on 1.1.1.1 on a website object called "happy"
and "sadwebsite.com" is hosted on 2.2.2.2 on a website object called "sad".
The home directory for both website objects "happy" and "sad" is
"C:\Inetpub\wwwroot\website". From the user's perspective, there are 2
distinct, unrelated websites. From my perspective, there is 1 website to
code and maintain, with 2 different skins.

I do this because "happywebsite.com" and "sadwebsite.com" require identical
functionality and only appear different via skin; however, each requires its
own SSL certificate matching the respective url; so AFAIK, while they can
reference the same code, they must also be hosted on seperate website
objects in IIS in order to assign them their own SSL cert.

So in the code, if Request.URL.Host contains "happy" or "sad", I use the
corresponding skin and the website takes on the appropriate visual identity
(and some other changes such as the company name, etc.) depending upon which
url the user visited.

This all works great until the user reaches the code somehow omitting the
"happy" or "sad" urls (i.e., they type either IP directly, or some
antiquated DNS entry erroneously resolves "fubar.com" to 1.1.1.1 or
2.2.2.2). It's rare, but technically I cannot rely on Request.URL.Host to
know which website I'm supposed to be "posing" as 100% of the time.

I could probably hard-code IPs 1.1.1.1 and 2.2.2.2 into code as a "plan B"
decision maker in the event the Host is unrecognized, but I would rather be
able to ask IIS which application or website object I am running because the
"happy" website object is always going to use the happy skin irrespective of
host name or IP address and seems to be the ultimate definitive check.

Try to use the HostingEnvironment Class (System.Web.Hosting Namespace)
There are following properties

Sitename
ApplicationId

The Sitename property returns a starting point name of the site and
ApplicationId - something like /w3svc/1/root
 
1388-2/HB said:
Title says it all.

Long version is that I have a win 2003 server box with 2 nics in it,
1.1.1.1 and 2.2.2.2, both hosting websites that reference the same code
location.

"happywebsite.com" is hosted on 1.1.1.1 on a website object called "happy"
and "sadwebsite.com" is hosted on 2.2.2.2 on a website object called
"sad". The home directory for both website objects "happy" and "sad" is
"C:\Inetpub\wwwroot\website". From the user's perspective, there are 2
distinct, unrelated websites. From my perspective, there is 1 website to
code and maintain, with 2 different skins.

I do this because "happywebsite.com" and "sadwebsite.com" require
identical functionality and only appear different via skin; however, each
requires its own SSL certificate matching the respective url; so AFAIK,
while they can reference the same code, they must also be hosted on
seperate website objects in IIS in order to assign them their own SSL
cert.

So in the code, if Request.URL.Host contains "happy" or "sad", I use the
corresponding skin and the website takes on the appropriate visual
identity (and some other changes such as the company name, etc.) depending
upon which url the user visited.

This all works great until the user reaches the code somehow omitting the
"happy" or "sad" urls (i.e., they type either IP directly, or some
antiquated DNS entry erroneously resolves "fubar.com" to 1.1.1.1 or
2.2.2.2). It's rare, but technically I cannot rely on Request.URL.Host to
know which website I'm supposed to be "posing" as 100% of the time.

I could probably hard-code IPs 1.1.1.1 and 2.2.2.2 into code as a "plan B"
decision maker in the event the Host is unrecognized, but I would rather
be able to ask IIS which application or website object I am running
because the "happy" website object is always going to use the happy skin
irrespective of host name or IP address and seems to be the ultimate
definitive check.

Try the following:

private string GetHostName(string Host)
{
UriHostNameType type = Request.Url.HostNameType;

if (type == UriHostNameType.IPv4 || type == UriHostNameType.IPv6) {
return System.Net.Dns.GetHostByAddress(Host).HostName;
} else {
return Host;
}
}

Pass in, as the parameter, Request.Url.Host.


Testing on my local machine, not remotely.

HTH,
Mythran
 
Back
Top