Server specific Web.config replacement

2019-08-26 01:20发布

I am currently using Visual Studio 2010. I want to set up a different connection string for my "Testing" Configuration. I have tried doing it by using config transformations, but it does not seem to work.

In my Web.config my connection string is the following:

<add name="MyDb" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DevelopmentDb;Integrated Security=SSPI; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

I found out that you can replace the connection string defined in Web.config by adding config transformation. I right clicked on Web.config and clicked on "Add config transformations". This created a Web.TEST.config file. I then added a connection string replacement, but it doesnt seem to work. It still uses the connection string defined in Web.config.

The file contents of Web.TEST.config is:

<?xml version="1.0"?>

<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of 
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator 
    finds an atrribute "name" that has a value of "MyDB".
  -->

    <connectionStrings>
      <add name="MyDb" 
        connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TestDb;Integrated Security=SSPI; MultipleActiveResultSets=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  <system.web>
    <!--
      In the example below, the "Replace" transform will replace the entire 
      <customErrors> section of your web.config file.
      Note that because there is only one customErrors section under the 
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

Any ideas to what I could be missing?

1条回答
贪生不怕死
2楼-- · 2019-08-26 02:09

Transformations get applied when you deploy the project, not when you compile it.

If you are using IIS, you can deploy to the configured web application directory, which will cause the transform to apply.

查看更多
登录 后发表回答