【问题标题】:Why can't I open a custom web.config file in my web ASP app?为什么我无法在我的 Web ASP 应用程序中打开自定义 web.config 文件?
【发布时间】:2014-07-16 13:29:26
【问题描述】:

我无法在我的 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 文件

【问题讨论】:

    标签: c# asp.net web-config


    【解决方案1】:

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

    【讨论】:

    • 是的,对不起,我现在用“/WebAuthorizationPilotageGeneral”再试一次,但结果还是一样..
    • 所以我相信你用错了。他们说“设置包含您要访问的 Web.config 文件的 Web 应用程序的根路径”。尝试将您的配置文件移动到新的子文件夹,将其命名为 web.config 并将您的路径设置为“/子文件夹”。我认为现在它正在加载您的完整 web.config。
    • 好的,我明白了。所以我把我所有的 web.config 文件放在一个文件夹中。并加载 ('/Authorization') ,就像我现在在描述中添加的图片一样。但结果还是一样。我想也许我们应该只按文件夹放置一个 web.config 文件。问题是我所有的网页都在根应用程序中
    • 是的,我相信是的。它总是加载文件名“web.config”。因此,您需要将您的授权配置放到不同的文件夹/子文件夹中,并将其命名为 web.config。但是将“完整”的 webconfig 保留在根目录中。希望它会工作。
    • 最后我改变了我的逻辑,就像你给我的建议一样。谢谢你的一切^^
    猜你喜欢
    • 2018-05-02
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多