如何獲取Web配置位置元素?如何獲取網頁配置位置元素?
ConfigurationManager.GetSection("appSettings") returns Okay
ConfigurationManager.GetSection("location") return null
I.E. ...
<location path="FOLDER/Page2.aspx">
...
</location>
如何獲取Web配置位置元素?如何獲取網頁配置位置元素?
ConfigurationManager.GetSection("appSettings") returns Okay
ConfigurationManager.GetSection("location") return null
I.E. ...
<location path="FOLDER/Page2.aspx">
...
</location>
你得到一個錯誤,造成這種情況的原因是因爲在.NET中,自定義應用程序配置部分(在您的示例中的「位置」部分如)要求您提供自定義配置節處理程序。
你需要使用的主界面是IConfigurationSectionHandler
這裏是如何創建一個custom configuration handler MSDN文章。
這有幫助嗎?
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(「〜」);' – 2013-04-29 23:55:26
兩者,這是因爲appSettings
是已知的(默認)配置在.NET應用程序部分。如果你想使用你自己的配置部分,你必須create it。
不知道這是否是你想要什麼,但你可以像這樣在web.config location元素中得到部分...
AuthorizationSection pageAuthorizationSection = (AuthorizationSection)WebConfigurationManager.GetSection("system.web/authorization", "FOLDER/Page2.aspx");
我想提高Neil的回答: 許多人說,不建議在運行時修改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");
}
這可能是一個醜陋的代碼,但肯定會起作用。 - 揚羅素「生鏽的程序員Calachan
它可以幫助,如果你能粘貼您的配置文件的全部內容。 – 2010-11-11 16:18:59