I stumbled across this the other day while working on a major CodeKeep upgrade (forthcoming shortly). There is a new property on the HttpRequest class named IsLocal which is a boolean flag that tells you if the current HTTP request is on the local machine or not. This is very handy because in pre-.NET 2.0 days we had to write that code ourselves by checking if the host was “localhost” or if the server IP address was 127.0.0.1, which is what the IsLocal property is actually doing (I checked with Reflector).
This is extremely useful to me with CodeKeep because I can put both my local database connection string and my production database connection string in web.config and, using HttpRequest.IsLocal, grab the appropriate connection string at runtime. For instance, my web.config contains the following <connectionStrings> section:
<
connectionStrings>
<add name="databaseDev" connectionString="..." providerName="System.Data.SqlClient" />
<add name="databaseProd" connectionString="..." providerName="System.Data.SqlClient" />
< FONT><connectionStrings>
I then have a helper class that contains the following static property:
public
static string ConnectionString
{
get
{
if (HttpContext.Current != null)
{
if (!HttpContext.Current.Request.IsLocal)
{
return ConfigurationManager.ConnectionStrings["databaseProd"].ConnectionString;
}
}
return ConfigurationManager.ConnectionStrings["databaseDev"].ConnectionString;
}
}
Just another addition to .NET 2.0 that makes development life a little bit nicer.
Print | posted on Saturday, December 17, 2005 7:01 PM