简体   繁体   English

使用configurationmanager从多个web.config文件中读取

[英]Using configurationmanager to read from multiple web.config files

Background: 背景:

I have some data thats stored in the web.config files of about 100 web applications. 我有一些数据存储在大约100个Web应用程序的web.config文件中。 This data is getting moved to a database gradually. 此数据逐渐转移到数据库。 The webpages will show the web.config data until somebody clicks on an "edit" link in which case they'll be redirected to a webpage which will allow them to update this data where it will be saved in a database instead. 网页将显示web.config数据,直到有人点击“编辑”链接,在这种情况下,他们将被重定向到一个网页,这将允许他们更新这些数据,而不是将其保存在数据库中。

Problem: 问题:

Not all of the data will be changed on this page that will save it to the database. 并非所有数据都将在此页面上更改,并将其保存到数据库中。 When somebody clicks the "edit" link I want the form to populate with the data from the web.config file and when they click "save" have it save to the database. 当有人点击“编辑”链接时,我希望表单填充来自web.config文件的数据,当他们点击“保存”时,它会保存到数据库中。 However, using the configurationmanager I can only get it to pull data from the web.config file on current application. 但是,使用配置管理器我只能从当前应用程序的web.config文件中提取数据。

Questions: 问题:

  1. Is there a way to use configurationmanager to select the web.config file from lets say ../{dynamic_app_id}/web.config ? 有没有办法使用configurationmanager来选择web.config文件,比如说../{dynamic_app_id}/web.config
  2. is reading them as plain xml files my only option? 将它们作为普通的xml文件读取我的唯一选择?
  3. Are there any pitfalls to this approach? 这种方法有什么缺陷吗?
  4. Is there another solution that would work better perhaps? 还有其他解决方案可能更好吗?

You can read any config file with ease. 您可以轻松读取任何配置文件。 Please see my sample code where I read application settings from external app.config file: 请参阅我的示例代码,其中我从外部app.config文件中读取应用程序设置:

        System.Configuration.KeyValueConfigurationCollection settings;
        System.Configuration.Configuration config;

        System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
        configFile.ExeConfigFilename = "my_file.config";
        config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
        settings = config.AppSettings.Settings;

Happy coding and best regards! 快乐的编码和最好的问候!

You can add below section in your web.config 您可以在web.config中添加以下部分

then, add "env" folder in your project and add your environmental settings into EnvironmentalSettings.config. 然后,在项目中添加“env”文件夹,并将环境设置添加到EnvironmentalSettings.config中。 And you can still use ConfigurationManager to get settings from EnvironmentalSettings file. 您仍然可以使用ConfigurationManager从EnvironmentalSettings文件中获取设置。

Does that answer your question? 这是否回答你的问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 努力在 ASP .NET 中使用 ConfigurationManager.AppSettings[“key”] 从 web.config 文件中读取值 - Struggling to read value from web.config file using ConfigurationManager.AppSettings[“key”] in ASP .NET 使用 ConfigurationManager 从 Web.Config 读取密钥 - Reading a key from the Web.Config using ConfigurationManager ConfigurationManager.ConnectionStrings["Test"].ConnectionString 未读取来自 Web.Config 的连接字符串 - Connection string from Web.Config not read by ConfigurationManager.ConnectionStrings["Test"].ConnectionString ConfigurationManager.AppSettings[Key] 是否每次都从 web.config 文件中读取? - Does ConfigurationManager.AppSettings[Key] read from the web.config file each time? 从web.config读取多个子配置 - Read multiple sub configurations from web.config 使用WebConfigurationManager从Web.config文件中读取appSettings部分 - Read appSettings section from Web.config file using WebConfigurationManager 无法在VS2022中使用ConfigurationManager访问web.config of ASP.NET Core Web API - Cannot access web.config of ASP.NET Core Web API using ConfigurationManager in VS2022 使用ConfigurationManager无法读取配置 - Using ConfigurationManager cannot read config 配置多个web.config文件 - Configuring multiple web.config files 从Web.Config读取变量 - Read Variable from Web.Config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM