【问题标题】:How get web config location element?如何获取网络配置位置元素?
【发布时间】:2011-05-08 13:52:11
【问题描述】:

如何获取网络配置位置元素?

ConfigurationManager.GetSection("appSettings") returns Okay

ConfigurationManager.GetSection("location") return null

I.E. ...

<location path="FOLDER/Page2.aspx">
...
</location>

【问题讨论】:

  • 如果您可以粘贴配置文件的全部内容,这可能会有所帮助。

标签: c# configuration web-config


【解决方案1】:

您收到错误的原因是因为在 .NET 中,自定义应用配置部分(例如示例中的“位置”部分)要求您提供自定义配置部分处理程序。

你需要使用的主界面是IConfigurationSectionHandler

这是一篇关于如何创建 custom configuration handler 的 MSDN 文章。

【讨论】:

    【解决方案2】:

    这有帮助吗?

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationLocationCollection myLocationCollection = config.Locations;
    foreach (ConfigurationLocation myLocation in myLocationCollection)
    {
        Console.WriteLine("Location Path: {0}", myLocation.Path);
        Configuration myLocationConfiguration = myLocation.OpenConfiguration();
        Console.WriteLine("Location Configuration File Path: {0}",              myLocationConfiguration.FilePath);
    }
    Console.WriteLine("Done...");
    Console.ReadLine();
    

    取自here

    【讨论】:

    • 这将适用于独立的可执行文件,即从 app.config 读取,但它不适用于 web.config。在这种情况下,第一行应替换为Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    【解决方案3】:

    这是因为appSettings 是 .NET 应用程序中的已知(默认)配置部分。如果你想使用自己的配置部分,你必须create it

    【讨论】:

      【解决方案4】:

      不确定这是否正是您想要的,但您可以像这样在 web.config 位置元素中获取部分...

      AuthorizationSection pageAuthorizationSection = (AuthorizationSection)WebConfigurationManager.GetSection("system.web/authorizati‌​on", "FOLDER/Page2.aspx");
      

      【讨论】:

        【解决方案5】:

        我想改进尼尔的回答: 许多人说不建议在运行时修改 web.config。但这里是如何做到这一点的代码。

           //The path where the web.config file is located
           string path = "~/Administrator/";
        
           //Collections of aspx page names separated by a comma. 
           //Example content in a textbox: Default.aspx,Audit.aspx,
        
           string strPages = txtPages.Text;
        
           //This is string array where we are going to break down all name of aspx pages 
           //contained in strPages variable
        
           string[] cdrPages = strValues.Split(',');
        
           //This is the list where we are going to transfer the names of our aspx pages 
           //for faster searching of existing items
        
           List<string> accesslist = new List<string>();
        
        
           try
                {
                    //1. Create Role
                    System.Web.Security.Roles.CreateRole(this.txtRoleName.Text);
        
                    //2. Open the Web Configuration --> make sure that you put the correct folder location of your web.config file
                    System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(path);
        
                    //3. Get All Specified Locations
                    ConfigurationLocationCollection myLocationCollection = config.Locations;
        
                    //4. Transfer the values of string[] strPages to List<string> accessList
                    for (int i = 0; i < strPages.Length; i++)
                    {
                        if (strPages[i].ToString() != null && strPages[i].ToString() != "")
                        {
                            accesslist.Add(strPages[i].ToString());
                        }
                    }
        
                    //5. Loop through the LocationCollections
                    foreach (ConfigurationLocation myLocation in myLocationCollection)
                    {
                        //6. Checks if myLocation exists in List<string> accessList
                        bool exists = accesslist.Exists(element => element == myLocation.Path); 
        
                        //If Exists
                        if (exists) {
        
                            //7. Open the configuration of myLocation
                            System.Configuration.Configuration sub = myLocation.OpenConfiguration();
        
                            //8. Get the authorization section of specific location
                            AuthorizationSection section = (System.Web.Configuration.AuthorizationSection)sub.GetSection("system.web/authorization");
        
                            //9. Declare the Authorization Rule, in this case, we are allowing a new role to have an access to a specific page
                            AuthorizationRule autho = new System.Web.Configuration.AuthorizationRule(System.Web.Configuration.AuthorizationRuleAction.Allow);
        
                            //10. Add the New Role to Authorization Section
                            autho.Roles.Add(this.txtRoleName.Text);
                            section.Rules.Add(autho);
        
                            //11. Save the "sub", or the specific location inside the web.config file.
                            sub.Save();
                        }
                    }
                        message.InnerHtml = "Role Successfully Added!";
                        message.Attributes.Add("class", "msg_info");
                }
                catch {
                        message.InnerHtml = "Saving Failed";
                        message.Attributes.Add("class", "msg_error");
                }
        

        这可能是一个丑陋的代码,但它肯定会工作。 - Jan Russel '生锈的程序员' Calachan

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多