我从后面的代码中需要更新我的web.config。这以前从来都不是问题,但是最近我遇到了一个错误。错误消息为“无法映射路径'/'。”
注释掉的行是我尝试过的不同变体。
//Configuration myWebConfig = WebConfigurationManager.OpenWebConfiguration(Server.MapPath("~"));
//Configuration myWebConfig = WebConfigurationManager.OpenWebConfiguration("~");
//Configuration myWebConfig = WebConfigurationManager.OpenWebConfiguration(null);
Configuration myWebConfig = WebConfigurationManager.OpenWebConfiguration(".");
// update pages theme
RoleManagerSection section = (RoleManagerSection)myWebConfig.GetSection("system.web/roleManager");
section.DefaultProvider = "SqlRoleManager";
section.Providers.Clear();
ProviderSettings providerSettings = new ProviderSettings();
providerSettings.Name = "SqlRoleManager";
providerSettings.Type = "System.Web.Security.SqlRoleProvider";
providerSettings.Parameters.Clear();
providerSettings.Parameters.Add("connectionStringName", "SimpleTickConnection");
providerSettings.Parameters.Add("applicationName", "TheaterSales");
section.Providers.Add(providerSettings);
myWebConfig.Save();
最佳答案
我找出了错误的原因。将站点从本地C:驱动器移至西方数字护照并运行该应用程序后,错误开始。下面的代码行很好:
Configuration myWebConfig = WebConfigurationManager.OpenWebConfiguration("~/");