Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to manage test/prod connection strings in .NET web.config file [duplicate]

Tags:

c#

.net

I have the source code of an application that I maintain. In my web.config file, I have connection strings for both my test and production databases as follows:

<add name="conn" connectionString="Data Source=TestDBServer; (etc...)">

<add name="conn" connectionString="Data Source=ProdDBServer; (etc...)">

Whenever I am in test, I just comment out the production connection string and vice-versa. For example, when I finish testing, I comment out the test connection string and uncomment the production connection string and then deploy.

I am very early on in my career as a developer, but it seems to me that there must be a better or more standard way to handle this. What should I do?

Edit

Guilherme Lofrano Corneto linked an identical question that completely answers my own question. I have since marked mine as a duplicate. Here is the link:

Change connection string from development to production when publishing

like image 240
Jake Avatar asked Apr 10 '17 12:04

Jake


People also ask

Is it safe to store connection string in Web config?

config based connectionstring as seems is unsafe, because one can read it. But think about it, if a person can read your web. config, means he can edit any file on your server anyways as he probably already hack or gain access to file. So, it really doesn't matter much.

Where should you store the connection string information?

Connection strings in configuration files are typically stored inside the <connectionStrings> element in the app. config for a Windows application, or the web. config file for an ASP.NET application.

Where do I put connection string in Web config?

<connectionStrings> <add name="dbconnection" connectionString="Data Source=Soumalya;Integrated Security=true;Initial Catalog=MySampleDB" providerName="System. Data. SqlClient" />


1 Answers

Visual Studio can automatically transform your web.config based on your currently active build configuration. So you should have a build configuration "Test", "Dev", "Prod" etc. (whatever you need for your workflow).

Then you can right-click on your web.config and click "Add transform". This creates a new config like web.Prod.config where you can overwrite your config values. When building with a specific build configuration, visual studio will automatically overwrite the default web.config with your corresponding transformation.

If you need to apply such transformations to other files than the web.config you might want to check extensions like "SlowCheetah - XML Transforms"

like image 67
Nasto Avatar answered Sep 29 '22 17:09

Nasto