Reputation: 177
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.
Upvotes: 1
Views: 2000
Reputation: 177
The answer is: "configPath" !!!!! It should be like this:
string configPath = "~";
Upvotes: 1