服务器特定的 Web.config 替换 [英] Server specific Web.config replacement

查看:31
本文介绍了服务器特定的 Web.config 替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是 Visual Studio 2010.我想为我的测试"配置设置不同的连接字符串.我曾尝试通过使用配置转换来做到这一点,但它似乎不起作用.

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.

在我的 Web.config 中,我的连接字符串如下:

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" />

我发现您可以通过添加配置转换来替换 Web.config 中定义的连接字符串.我右键单击 Web.config 并单击添加配置转换".这创建了一个 Web.TEST.config 文件.然后我添加了一个连接字符串替换,但它似乎不起作用.它仍然使用 Web.config 中定义的连接字符串.

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.

Web.TEST.config 的文件内容是:

<?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?

推荐答案

转换在您部署项目时应用,而不是在编译时应用.

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

如果您使用的是 IIS,则可以部署到配置的 Web 应用程序目录,这将导致应用转换.

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

这篇关于服务器特定的 Web.config 替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆