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
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
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.
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?
Try this:
HttpRequest httpRequest = HttpContext.Current.Request;
string host = httpRequest.Headers["host"];
IPAddress hostAddresse = Dns.GetHostAddresses("host").Where(a=>a.AddressFamily ==AddressFamily.InterNetwork).FirstOrDefault();