Link to home
Start Free TrialLog in
Avatar of whityum
whityum

asked on

ConfigurationSection properties cannot be edited when locked

I'm trying to add URL Mappings whenever a new page is added to the database, this is the code I have, but it keeps erroring out:


                  System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
                  UrlMappingsSection ums = (UrlMappingsSection)configuration.GetSection("system.web/urlMappings");
                  UrlMappingCollection umc = ums.UrlMappings;
                  UrlMapping um = new UrlMapping("~/" + sUrl, pageURL + "?pageID=" + pageID.ToString());            
                  umc.Add(um);
                  configuration.Save(ConfigurationSaveMode.Full);

Network Service and ASPNET both have read/write access to the file.

The error is:
Exception Details: System.InvalidOperationException: ConfigurationSection properties cannot be edited when locked.

on the configuration.Save() line
Avatar of Elvio Lujan
Elvio Lujan
Flag of Argentina image

you can't modify the web.config file this way...
you need to edit the file and save overwiting it
Avatar of whityum
whityum

ASKER

This code is from the microsoft site:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   Dim myconf As Configuration = WebConfigurationManager.OpenWebConfiguration("/ASPNET20CC")
   Dim myUrls As UrlMappingsSection
   Dim myGroup As System.Web.Configuration.SystemWebSectionGroup = myconf.GetSectionGroup("system.web")
   myUrls = myGroup.UrlMappings
   myUrls.UrlMappings.Add(New UrlMapping(txtUrl.Text, txtZiel.Text))
   If Not myUrls.LockItem Then
            myconf.Save()
   End If
End Sub

I converted it to C#, and even attempted to unlock the UrlMappingsSection and it still says ConfigurationSection properties cannot be edited when locked

            Configuration myconf = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
            System.Web.Configuration.SystemWebSectionGroup swsg = (SystemWebSectionGroup)myconf.GetSectionGroup("system.web");
            UrlMappingsSection ums = swsg.UrlMappings;
            ums.LockItem = false;
            UrlMapping um = new UrlMapping("~/" + sUrl, pageURL + "?pageID=" + pageID.ToString());            
            ums.UrlMappings.Add(um);                  
            ums.SectionInformation.ForceSave = true;
            myconf.Save(ConfigurationSaveMode.Full);
            return true;

Has anyone added URL Mappings in code successfully?
Avatar of whityum

ASKER

and lem2802, why do you say that?

there is a whole management API to edit the configuration
http://asp.net/QuickStart/aspnet/doc/management/mgmtapi.aspx
Avatar of whityum

ASKER

I changed Configuration myconf = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); to

Configuration myconf = WebConfigurationManager.OpenWebConfiguration("/projectName");

and it works.
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial