繁体   English   中英

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

[英]Why can't I open a custom web.config file in my web ASP app?

我无法在我的 ASP Web 项目中打开 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 个部分,并且应该只有一个 => "authorization" ,它看起来像我的 (web.config) 根文件而不是 (WebAuthorizationPilotageGeneral) 配置文件。

在此处输入图片说明

更新:我尝试将我的 4 个配置文件放在一个文件夹中在此处输入图片说明

Web.config 文件在此处输入图片说明

在 MS 示例中,使用了没有“~”符号的相对配置路径。 你有没有尝试删除它?

// 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM