2010-11-11 36 views
5

Cách nhận yếu tố vị trí cấu hình web?Cách lấy yếu tố vị trí cấu hình web?

ConfigurationManager.GetSection("appSettings") returns Okay 

ConfigurationManager.GetSection("location") return null 

I.E. ...

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

Nó có thể giúp đỡ nếu bạn có thể dán toàn bộ nội dung tập tin cấu hình của bạn. –

Trả lời

1

Lý do bạn nhận được một lỗi cho điều đó là bởi vì trong .NET, phần tùy chỉnh ứng dụng cấu hình (ví dụ như "vị trí" trong ví dụ của bạn) yêu cầu bạn phải cung cấp một tùy chỉnh phần xử lý cấu hình.

Giao diện chính, bạn cần phải sử dụng là IConfigurationSectionHandler

Dưới đây là một bài viết MSDN về cách tạo custom configuration handler của bạn.

5

Điều này có hữu ích không?

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

Taken từ here

+1

Điều này sẽ làm việc cho các tập tin thực thi độc lập, tức là đọc từ app.config, nhưng nó sẽ không hoạt động đối với web.config. Trong trường hợp này, dòng đầu tiên phải được thay thế bằng 'Cấu hình cấu hình = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration (" ~ ");' –

0

này là bởi vì appSettings là một (mặc định) phần cấu hình được biết đến trong một ứng dụng .NET. Nếu bạn muốn sử dụng phần cấu hình của riêng bạn, bạn phải create it.

4

Không chắc nếu điều này là chính xác những gì bạn muốn nhưng bạn có thể nhận được phần bên trong phần tử vị trí web.config như vậy ...

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

Tôi muốn cải thiện câu trả lời của Neil: Nhiều người nói rằng sửa đổi web.config của bạn tại thời gian chạy không được khuyến khích. Nhưng đây là mã làm thế nào để làm điều đó.

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

Đây có thể là mã xấu, nhưng chắc chắn nó sẽ hoạt động. - Jan Russel 'Rusty Programmer' Calachan

Các vấn đề liên quan