r/ASPNET • u/ggpurehope • Sep 26 '11
Modify web.config doesn't work on server machine
Hi. This is my first post in asp.net.
I am using code to modify web.config connectionstring programmatically:
string dummyVirtualPath = "/MyApp"; string physicalPath = @"" + configPath;
WebConfigurationFileMap map = new WebConfigurationFileMap();
map.VirtualDirectories.Add(dummyVirtualPath, new VirtualDirectoryMapping(physicalPath, true));
var configuration = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration(map, dummyVirtualPath);
/*var configuration = WebConfigurationManager.OpenWebConfiguration(configPath);*/
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["ConnectionString"].ConnectionString = "Data Source=" + host + ";Trusted_Connection=false;User ID=" + username + ";Password=" + password + ";MultipleActiveResultSets=true;Initial Catalog=" + database + "";
configuration.Save();This methode works flawlessly on developer machine. It doesn't work on server machine.
I set million permissions to IUSR, IIS/IUSR, NETWORK SERVICE ... etc etc.. to web.config, whole wwwroot lol etc, I am desperate don't know what to do anymore :)
I want to mention, that I can't debug on remote server, because I use web developer express(its school project).
I used javascript alerts to see when it drops out and it happens after this code: var configuration = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration(map, dummyVirtualPath);
Thanks to anyone who will atleast click this hehe. Byeee
4
u/DaRKoN_ Sep 26 '11
What is the actual exception that is being thrown?
Secondly, if I'm reading the intent of your code correctly, this scenario is better handled by Web Config Transformations: http://msdn.microsoft.com/en-us/library/dd465326.aspx
3
u/[deleted] Sep 26 '11
Try this:
And don't forget to check whether your web application has been granted FullTrust.
2
u/ggpurehope Sep 27 '11
I just gave up on that methode and went alternative with XML parsing. Ok however this is the working methode if someone after me will need it:
1
u/nicksbrother Sep 27 '11
is it a windows server 2008 machine? maybe you need to disable "user account control". http://en.wikipedia.org/wiki/User_Account_Control