1

This is my global.asax.cs file in my ASP.NET MVC application. I want to programmatically modify an attribute in web.config. But this error occurred:

An error occurred executing the configuration section handler for system.web/roleManager.

Code:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MyApp
{    
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            UsingRoleManagerSection.test();
        }
    }

    public static class UsingRoleManagerSection
    {
        public static void test()
        {
            try
            {
                // Set the path of the config file.
                string configPath = "/Web.config";

                // Get the Web application configuration object.
                Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

                SystemWebSectionGroup web = (SystemWebSectionGroup)config.GetSectionGroup("system.web");


                web.ForceDeclaration(true);
                web.RoleManager.Enabled = true; 
                web.RoleManager.SectionInformation.ForceSave = true;              
                config.Save(ConfigurationSaveMode.Modified);
            }
            catch (Exception ex)
            {

            }
        }
    }
}

The RoleManager.SectionInformation.IsLocked is true. I did try to change it to false by set:

configSection.SectionInformation.AllowDefinition = ConfigurationAllowDefinition.Everywhere;
configSection.SectionInformation.AllowOverride = true;

but in first line this error occurred:

ConfigurationSection properties cannot be edited when locked.

1 Answer 1

1

The answer is: "configPath" !!!!! It should be like this:

string configPath = "~";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.