• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2619
  • Last Modified:

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
0
whityum
Asked:
whityum
  • 3
1 Solution
 
Elvio Lujan.Net Senior DeveloperCommented:
you can't modify the web.config file this way...
you need to edit the file and save overwiting it
0
 
whityumAuthor Commented:
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?
0
 
whityumAuthor Commented:
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
0
 
whityumAuthor Commented:
I changed Configuration myconf = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); to

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

and it works.
0
 
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now