web.config 文件部分替换

发布于 2024-07-22 10:09:49 字数 178 浏览 10 评论 0原文

我有一个 Web 部署项目,它使用外部文件替换 web.config 部分。 (这是为了更改连接字符串部分)。 手动构建时,web.config 部分替换工作正常,但当作为 TFS 构建的一部分构建时,该部分不会被替换。 我在构建日志中找不到任何错误或警告。

可能的原因是什么,或者我该如何“调试”这个问题?

I have a web deployment project that does a web.config section replacement using an external file. (this is to change the connection strings section).
The web.config section replacement works fine when built manually, but when built as part of a TFS build the section is not replaced. I cannot find any errors or warnings in the build log.

What are the likely causes, or how can I 'debug' this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长途伴 2024-07-29 10:09:50

您是否考虑过使用 Web.Config 的功能从单独的文件中提取部分? 您像这样引用外部文件(这是我用于加载具有连接字符串部分的文件的代码):

<connectionStrings configSource="WebCS.config"/>

然后可以将连接字符串部署为单独的文件:

 <connectionStrings>
       <add name="ConnString" connectionString="Data Source=<server>;Initial Catalog=<DB>;User ID=<ID>;Password=<pwd>" providerName="System.Data.SqlClient"/>
 </connectionStrings>

这样,您不必担心更改web.config 文件根本没有。

Have you considered using Web.Config's ability to pull a section from a separate file? You refer to the external file like so (this is my code for loading a file that has my connection strings section):

<connectionStrings configSource="WebCS.config"/>

Then the connection string can be deployed as a separate file:

 <connectionStrings>
       <add name="ConnString" connectionString="Data Source=<server>;Initial Catalog=<DB>;User ID=<ID>;Password=<pwd>" providerName="System.Data.SqlClient"/>
 </connectionStrings>

That way, you don't have to worry about changing the web.config file at all.

欢烬 2024-07-29 10:09:50

我不确定这是否有帮助......但这是一种添加/更新连接字符串而无需替换整个配置部分的方法。

  public static void SaveConfigVal(string connectionString, string connName)
        {

            System.Configuration.ExeConfigurationFileMap fileMap = new System.Configuration.ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = GetConfigFileName();
            //System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            // Retrieve the section group
            ConnectionStringSettings keyValue = config.ConnectionStrings.ConnectionStrings[connName];

            // If the key already exists, just replace
            if (keyValue != null)
            {
                keyValue.ConnectionString = connectionString;
            }
            else
            {
                // Add a new key if the setting doesn't exist
                config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(connName, connectionString));
            }

            config.Save(ConfigurationSaveMode.Modified);// (ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("ConnectionStrings");
        }

        private static string GetConfigFileName()
        {
            //return config file name....
        }

I am not sure this will help at all....but this is a way to add/update a connection string without having to replace the whole config section.

  public static void SaveConfigVal(string connectionString, string connName)
        {

            System.Configuration.ExeConfigurationFileMap fileMap = new System.Configuration.ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = GetConfigFileName();
            //System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            // Retrieve the section group
            ConnectionStringSettings keyValue = config.ConnectionStrings.ConnectionStrings[connName];

            // If the key already exists, just replace
            if (keyValue != null)
            {
                keyValue.ConnectionString = connectionString;
            }
            else
            {
                // Add a new key if the setting doesn't exist
                config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(connName, connectionString));
            }

            config.Save(ConfigurationSaveMode.Modified);// (ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("ConnectionStrings");
        }

        private static string GetConfigFileName()
        {
            //return config file name....
        }
微暖i 2024-07-29 10:09:50

如果您正在使用或可以升级到 Visual Studio 2010,则可以利用新的 web.config 转换来根据所选配置更改 web.config。

http://msdn.microsoft.com/en-us/library/dd465318.aspx

If you're using or can upgrade to Visual Studio 2010, you can utilize the new web.config transformations to alter the web.config based on selected configuration.

http://msdn.microsoft.com/en-us/library/dd465318.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文