1

I'd like to do this in the Application_Start method, so I can write a 'robots.txt' file if the site is running on a test server.

Thanks

Nick
  • 5,616
  • 10
  • 52
  • 72
  • The webserver's ip address is not in the request object, and nor is it in a variable called UserHostAddress. What made you think that? – Klaus Byskov Pedersen Nov 16 '10 at 09:48
  • IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); http://www.csharp-examples.net/local-ip/ – Kell Nov 16 '10 at 09:50
  • @Kell this might not work. A web server could have many IP addresses and you need to find out which one this website is binding to. SO you have to use host header. – Aliostad Nov 16 '10 at 09:57
  • @Klaus - you're right, I'm not making any sense on that one.. – Nick Nov 16 '10 at 10:18
  • possible duplicate of [C# ASP.net how to get the server IP Address?](http://stackoverflow.com/questions/1345676/c-asp-net-how-to-get-the-server-ip-address) – Zhaph - Ben Duguid Nov 16 '10 at 11:24
  • Also bear in mind that a website can be bound to many hosts! – Rippo Nov 17 '10 at 09:42
  • It's not a duplicate of the other question, which is *not* about global.asax. I have a similar issue, and @Kell's answer works fine, thanks. – philw Jul 04 '12 at 17:20

3 Answers3

1

Try this.

var ips = Dns.GetHostAddresses(Dns.GetHostName());
if (ips.Any())
{
    var serverIp = ips[ips.Count()-1];
}

Dns.GetHostAddresses will return different length due to local or remote server,

that's why i use ips.Count()-1 to get the least one.

Chieh
  • 128
  • 1
  • 2
1

I originally thought old school, using the ServerVariables collection, as the following worked nicely in my test app, but running under the built in VS web server:

void Application_Start(object sender, EventArgs e) {
  // Code that runs on application startup
  string localAddress = Context.Request.ServerVariables["LOCAL_ADDR"];
  // Returns "::1" when running under the VS server, however it throws an 
  // exception under IIS Express, so I assume it also does so under IIS.
}

The next best option I can come up with would be something like:

void Application_Start(object sender, EventArgs e) {
  // Code that runs on application startup
  string serverName = Server.MachineName;
}

Which works on both VS and IIS express, so if you know the name of your test or live servers, you could check against that instead?

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
  • The question asks about the global.asax Application_Start method. I think you'll find there's no "server address on which the request came in" because there's no specific request. So that's going to silently crash on you. Same problem with the answer below. – philw Jul 04 '12 at 17:11
0

Try this:

        HttpRequest httpRequest = HttpContext.Current.Request;
        string host = httpRequest.Headers["host"];
        IPAddress hostAddresse = Dns.GetHostAddresses("host").Where(a=>a.AddressFamily ==AddressFamily.InterNetwork).FirstOrDefault();
Aliostad
  • 80,612
  • 21
  • 160
  • 208