4

I would like to know how to check if i am using my localhost, or on the live server.

Basically i would like to have...

if (using localhost)
{
  Do Stuff...
}
else
{
   Do this instead...
}

How would i go about doing this? I have searched around but cant find anything. I would like to do this as i have different settings for the live and the dev servers. And i would like a way to automatically check to see what i am using, and then use certain settings.

Thanks in advance.

thatuxguy
  • 2,418
  • 7
  • 30
  • 51
  • 1
    Though @COLDTOLD's answer is quite sufficient (+1'd), if you are dealing with static settings that are stored in your config, you may be interested to know that there is something referred to as config transformations. Essentially you have a single config file, and depending on your deployment, various settings in your config can be automatically replaced with its respective information. Here's a [How To](http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx) on the topic. A very common use would be to replace database connection information. – Jeremy Sep 17 '12 at 16:15
  • That look like an interesting solution, i will look into it more :D – thatuxguy Sep 17 '12 at 16:42

3 Answers3

17

you can try doing something like this

HttpApplication http = new HttpApplication();
if (http.Request.IsLocal)
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
5
if(HttpContext.Current.Request.IsLocal)
{
}
Alex Z
  • 1,362
  • 15
  • 16
  • 1
    Word of warning: this can't be used in `Application_Start()`. See http://stackoverflow.com/questions/2518057/request-is-not-available-in-this-context – Alexander May 25 '16 at 16:57
3

Depending on whether you really need the check to be about the location, or about the build type, you might be interested in decorating the code with DEBUG checks.

See the following link.

http://msdn.microsoft.com/en-us/library/4y6tbswk(v=vs.100).aspx

#if DEBUG
 // Do stuff
#else
 // Do other stuff
#endif
J. Tanner
  • 575
  • 2
  • 10