MatiasP
MatiasP

Reputation: 69

How to manage different web.x.config files

I have been trying to google and change and in the mode of hating this right now so help me please.

I have a web.config looks like this:

    <configuration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
  </connectionStrings>

And in my web.Debug.config:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="ApplicationServices"
      connectionString="value for the deployed Web.config file"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

I then run the "Debug", and want to access the configuration in the web.Debug.config and NOT the web.config. But it always returns the web.config, what i'm doing wrong? The code for retreiving looks like this:

ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString

Upvotes: 0

Views: 76

Answers (1)

Markus
Markus

Reputation: 22511

The web.*.config files are for transforming your web.config and therefore are called "Web.Config Transformations".

The transformations are only run when you publish/deploy the web application, not by just debugging your application. That's the reason why the web.Debug.config in particular is rarely used (maybe for a test system if you don't use another configuration for that).

In order to use special debug settings, follow this approach:

  1. Adjust your web.config file so that it contains the settings for debugging.
  2. Adjust the web.Release.config file so that it replaces the debug settings with the settings you want to use after you've published the web application.
  3. When deploying your application, do not just copy the build output, but perform a real deployment (maybe to the file system if you can't update the server directly). Make sure that you use the Release configuration when deploying so that the transformations are applied.

Upvotes: 3

Related Questions