为什么我无法在我的Web ASP应用程序中打开自定义web.config文件?

时间:2014-07-16 13:29:27

标签: c# asp.net web-config

我不知道为什么,但我无法在我的ASP web projet中打开web.config文件。 我在项目中添加配置文件以设置授权页面。

我需要打开这些页面,以便从我的管理页面更新“角色”名称。

我按照微软的样本,它应该正常工作:

http://msdn.microsoft.com/en-us/library/vstudio/4c2kcht0(v=vs.100).aspx

这是我的代码C#我使用:

//Open the web.config custom
Configuration config  = WebConfigurationManager.OpenWebConfiguration("~/WebAuthorizationPilotageGeneral") as Configuration;

//Get the authorization section

// section is always == NULL !!!!

-> section = (System.Web.Configuration.AuthorizationSection)config.GetSection("authorization");

//Remove all Roles before add news Roles
section.Rules.Clear();
autho = new System.Web.Configuration.AuthorizationRule(System.Web.Configuration.AuthorizationRuleAction.Allow);
foreach (string role in Roles.GetAllRoles())
{
     if (role != ttbx_RoleName.Text)
     {
          autho.Roles.Add(role);
     }
}

//Add the news Roles
section.Rules.Add(autho);
//Save the web.config custom
config.Save(ConfigurationSaveMode.Full, true);

我的web.config自定义文件我尝试加载更新“授权”部分

<!-- WebAuthorizationPilotageGeneral.config - access pilotage page -->
<authorization>
  <allow roles="Administrator" />
  <allow roles="Audit" />
  <allow roles="Copil" />
  <allow roles="System" />
  <allow roles="User" />
  <allow roles="visitor" />
  <deny users="*" />
</authorization>

奇怪的是在加载配置文件之后,我的配置对象仍然有22个部分,并且应该只有一个=&gt; “授权”,它看起来像我的(web.config)根文件,而不是(WebAuthorizationPilotageGeneral)配置文件。

enter image description here

更新:我尝试将我的4个配置文件放在一个文件夹enter image description here

Web.config文件 enter image description here

1 个答案:

答案 0 :(得分:0)

在MS示例中,使用了相对配置路径,没有&#34;〜&#34;符号。你试过删除吗?

// Set the root path of the Web application that contains the
// Web.config file that you want to access.
string configPath = "/MyAppRoot";

// Get the configuration object to access the related Web.config file.
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);