Use different connection strings depends on current URL

I manage two servers at work. One for development and the other one is production server. I used to have two different web.config file and have different connection string to the database in them. But what I should do is write an if statement to decide what to use depends on what the current URL is.

In <appSettings> tag of the “web.config” file, add the following:

<add key=”Staging” value=”; />
<add key=”DevelopmentConnectionString” value=”server=Dev-DB01; database=DBSample;Integrated Security=SSPI”/>
<add key=”ProductionConnectionString” value=”server=Dev-DB01; database=DBSample;Integrated Security=SSPI”/>

Create a Global Application Class file and name it “Global.asax“. In the file, add the following:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
If (Request.Url.Host = ConfigurationManager.AppSettings(“Staging”)) Then
Context.Items.Add(“ConnectionString”,ConfigurationManager.AppSettings( “DevelopmentConnectionString”))
context.Items.Add(“ConnectionString”,ConfigurationManager.AppSettings( “ProductionConnectionString”))
End If
End Sub


No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: