How to know when the application is running in Localhost or debugging mode

Hi,

Most of the time (If not all) when working on the project we view the website with the help of localhost in debugging mode. Many a times when working on some modules, for solving small problem, we want to write some code that should execute only when running in the localhost or debugging environment.

Normally developers would write the code and then take it as their responsibility to remove the code before the code goes to the next environment. But some time we might make a mistake of not removing the code and letting it go to the next environment, which can cause a lots of trouble.

In these kinds of circumstances and Asp.Net developer can take help of the IsLocal method of the Request class. The method will return true in case the application is running from localhost.

If (Request.IsLocal)
{
// Do task that should execute only when running in localhost.
}

To know if the application is running in the debugging mode, we can use the


If (HttpContext.Current.IsDebuggingEnabled)
{
// Do task that should execute only when Application is running in debugging mode.
}

Vikram


Share this post   Email it

Feedback

Posted on 9/17/2009 12:24:11 PM

Hi vikram is there any way to comment or uncomment based on environment in source I mean not in code behind.There may be many situations that we may want uncomment java script references when running in development & we may want to comment it when running in other environment

Posted on 9/17/2009 10:17:59 PM

HI Sandeep,

What you can do is keep all the js code inside a Placeholder or as a text of literal control. and based on the above condition make the literal or placeholder visible tru or false.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 




Copyright © 2006 - 2010 Vikram Lakhotia