简体   繁体   English

在C#中以编程方式更改REMOTE web.config

[英]Change a REMOTE web.config programmatically in C#

I have two web project hosted on same server that is One site User & Admin Projects.I want to dynamically change the values of App Setting (Key of app setting) 我在同一服务器上托管了两个Web项目,即一个站点的用户和管理项目。我想动态更改应用设置的值(应用设置的键)

I know how to change the value web.config value programmatically in Same Project.But how different project, 我知道如何在同一项目中以编程方式更改值web.config值。但是如何在不同的项目中,

System.Configuration.ExeConfigurationFileMap configFile = new System.Configuration.ExeConfigurationFileMap();
configFile.ExeConfigFilename = "ConsoleTester.exe.config";  //name of your config file, can be from your app or external
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFile, System.Configuration.ConfigurationUserLevel.None);
System.Configuration.KeyValueConfigurationCollection settings = config.AppSettings.Settings;
settings["DD"].Value = "007_Access";
config.Save();

What I get of your problem is, You have one codebase and you need to serve two application instance with different functionalities. 我得到的问题是,您拥有一个代码库,并且需要为两个具有不同功能的应用程序实例提供服务。

First of all its not good idea to modify your web.config at runtime. 首先,在运行时修改web.config并不是一个好主意。

To have two application instance based on one codebase with separated web.config, you can use virtual directory feature of IIS. 要具有基于一个单独的web.config的一个代码库的两个应用程序实例,可以使用IIS的虚拟目录功能。 create two web-application on IIS with two web.config(separated directory) and then define a virtual directory pointed to your codebase. 在IIS上使用两个web.config(分隔目录)创建两个Web应用程序,然后定义一个指向您的代码库的虚拟目录。

In IIS 7+ there is a C# API that allows you to create applications, and modify configs. 在IIS 7+中,有一个C#API,可让您创建应用程序和修改配置。

The documentation is here Microsoft.Web.Administration 该文档在此处Microsoft.Web.Administration

I think this code should get you there abouts: 我认为这段代码应该可以帮助您:

   var serverManager = new ServerManager();
   var site = serverManager.Sites["SiteName"];
   var app = site.Applications["MyAppName"];
   var config = app.GetWebConfiguration();
   var section = config.GetSection("appSettings");
   var element = section.GetChildElement("mySettingKey");
   element.SetAttributeValue("value", "MyNewValue");
   serverManager.CommitChanges();

You can make a app_offline.htm file which will take your site down. 您可以制作一个app_offline.htm文件,该文件将使您的网站app_offline.htm All you have to do is place the file in the root directory of your website and IIS will do the rest for you. 您所要做的就是将文件放在网站的根目录中,然后IIS会为您完成其余工作。 You can add a maintenance message on the app_offline.htm and do maintenance while the users will see only the app_offline.htm page 您可以在app_offline.htm上添加维护消息并进行维护,而用户只会看到app_offline.htm页面

If you would really wanna go programmatically. 如果您真的想通过编程去。 You can rename the file to something else when its not needed and rename it back to app_offline.htm when you wanna take the site down. 您可以在不需要该文件时将其重命名为其他名称,并在需要关闭站点时将其重命名为app_offline.htm

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM