在 Web 安装程序自定义操作期间,我正在修改 Web.config 文件上的连接字符串。
这是正在做这项工作的代码片段
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = path;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
var connectionsection = config.ConnectionStrings.ConnectionStrings;
ConnectionStringSettings connectionstring = connectionsection[connStringName];
if (connectionsection != null)
connectionsection.Remove(connStringName);
connectionstring = new ConnectionStringSettings(connStringName, newValue, "System.Data.SqlClient");
connectionsection.Add(connectionstring);
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
到目前为止一切顺利,这实际上是有效的,但它也在“system.web”部分添加了一些项目,这些项目导致了错误:
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: It is an error to use a section registered as allowDefinition='MachineOnly' beyond machine.config.
Source Error:
Line 46: <authorization />
Line 47: <clientTarget />
Line 48: <deployment />
Line 49: <deviceFilters />
Line 50: <fullTrustAssemblies />
当我手动删除 ConfigurationManager 添加的某些部分时,<deployment />
错误就消失了。所以我只需要 ConfigurationManager 不创建这些部分。怎么做?<protocols />
<processModel />
谢谢