2014-07-16 17 views
1

我不知道爲什麼,但我無法打開我的ASP web projet中的web.config文件。 我在我的項目中添加配置文件以設置授權頁面。爲什麼我無法在我的網頁ASP應用程序中打開自定義的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> 

怪異的東西是加載配置文件後,我的配置objet仍然有22個部分,應該只有一個=>「授權」,它看起來像我的(web.config)根文件,而不是(WebAuthorizationPilotageGeneral )配置文件。

enter image description here

更新:我試圖把只在一個文件夾,我的4個配置文件 enter image description here

Web.config文件 enter image description here

回答

0

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

是的,對不起,我剛纔再試一次以「/ WebAuthorizationPilotageGeneral」,但還是同樣的結果.. –

+0

所以我相信你使用它走錯了路。他們說:「設置包含您要訪問的Web.config文件的Web應用程序的根路徑。」嘗試將您的配置文件移動到新的子文件夾,將其稱爲web.config,並將您的路徑設置爲「/子文件夾」。我想現在它正在加載完整的web.config。 –

+0

好吧,我明白了。所以我把我所有的web.config文件放在一個文件夾中。並加載('/ Authorization'),就像我剛剛添加的描述一樣。但仍然相同的結果..我想也許我們應該只放一個web.config文件的文件夾。問題是我的所有網頁都在根應用程序 –

相關問題