这是我的ASP.NET MVC应用程序中的global.asax.cs
文件。我想以编程方式修改web.config
中的属性。但是发生了这个错误:
执行system.web / roleManager的配置节处理程序时发生错误。
代码:
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)
{
}
}
}
}
RoleManager.SectionInformation.IsLocked
是真的。我确实尝试通过设置将其更改为false:
configSection.SectionInformation.AllowDefinition = ConfigurationAllowDefinition.Everywhere;
configSection.SectionInformation.AllowOverride = true;
但在第一行发生了这个错误:
锁定时无法编辑ConfigurationSection属性。
答案 0 :(得分:0)
答案是:" configPath" !!!!! 它应该是这样的:
string configPaht = "~";