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

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
  • 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
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
   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.SectionInformation.ForceSave = true;
            return true;

Has anyone added URL Mappings in code successfully?
whityumAuthor Commented:
and lem2802, why do you say that?

there is a whole management API to edit the configuration
whityumAuthor Commented:
I changed Configuration myconf = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); to

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

and it works.
PAQed with points refunded (500)

Community Support Moderator

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