Code Behing update web.config mapping issue

孤人 提交于 2019-12-10 20:21:51

问题


I from the code behind I need to update my web.config. This has never been a problem before, however I am getting an error recently. The errors say "Failed to map the path '/'." The lines commented out were different variations of what i tried.

//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();

回答1:


I figured out the reason for the error. After moving the site from my local C: drive to a western digital passport and running the app the error started. The line of code below is fine:

Configuration myWebConfig = WebConfigurationManager.OpenWebConfiguration("~/");



回答2:


Just at first glance, try making WebConfigurationManager.OpenWebConfiguration("."); point to an actual configuration file such as web.config

WebConfigurationManager.OpenWebConfiguration("web.config");

Make sure to also include the path if needed.




回答3:


The file path "~/" means the root folder of the Web application. "~" and null don't have any meaning to the method; "." means the current (i.e., bin) folder.




回答4:


It happened to me when I switched from Windows Vista to Windows 7. The default security of Windows 7 caused this because VS didn't have permissions to open the file. Just I restarted Visual Studio as Administrator and it worked, as simple as that. I hope it helps some other people.



来源:https://stackoverflow.com/questions/1447160/code-behing-update-web-config-mapping-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!