How to have different web.config settings for my local machine?

12,586

Solution 1

You need to add a solution configuration in order to add web.config transformations.

  • Right click on your solution (in solution explorer)
  • Select "Configuration manager" and in the "Active solution configuration" dropdown, click on new.
  • Enter a configuration name (like "local" for instance)
  • Copy settings from debug (seems more appropriate for development)
  • Check "Create new projects configuration".

Set your projects configuration aswell, and when you're done, right click on the web.config, select "add transforms" and you're good to go !

Then, when you want to use your local connection string, just use this configuration instead of debug.

Solution 2

It depends on how you debug. If you are using Cassini, afaik your web.config contents will be read regardless of the selected solution configuration (e.g. Debug or Release).

If you are debugging with your local IIS, it depends on what you have set the path in the IIS to. If you have set it to your source code directory, you need to write your local settings into your web.config. If you publish your code into a local directory and set the IIS path accordingly, you can use web.config transforms. (You said, that your web.config transforms are working)

For other future readers:
I'd recommend the following: Have your debug settings in your web.config. Create a solution configuration with "Release" settings (Web.Release.Config), which you use for your publish process.

Check out http://www.tomot.de/en-us/article/5/asp.net/how-to-use-web.config-transforms-to-replace-appsettings-and-connectionstrings for a step by step tutorial for web.config transforms with VS 2010. Although the publish dialog changed in VS 2012, you should be able to adapt the guide to the newer version.

Share:
12,586
RobVious
Author by

RobVious

Updated on June 18, 2022

Comments

  • RobVious
    RobVious almost 2 years

    I'm finding myself having to manually update my DefaultConnection connection string in my web.config when I run locally. How can I automatically detect when I'm running locally and - when I am - overwrite the default connection string?

    I have debug/release transformations working, but these are for deployment. I'm looking for a way to add another option - "local" - if there's any way to do it (or something like it).

  • RobVious
    RobVious over 10 years
    Thank you! Though now I'm running into a problem where my local transformations are not getting applied. I've asked a separate question here in case you have a minute: stackoverflow.com/questions/19301002/…
  • Superzadeh
    Superzadeh over 10 years
    Are you using Cassini or IIS or IIS Express ? @Citronas gave explanations about this right below: stackoverflow.com/a/19294757/375304
  • citronas
    citronas over 10 years
    Why is this answer accepted? The OP stated that he has web.config transforms working and this answer describes how to create web.config transforms
  • Christian Kuetbach
    Christian Kuetbach about 10 years
    The provided link was really useful.