2011-09-26 41 views
0

我有這樣的代碼在我的web.config文件:在一個web.config Retriving設置<configSections>

<configSections> 
     <section name="myWebAppSettings" type="System.Configuration.SingleTagSectionHandler" /> 
    </configSections> 
    <myWebAppSettings isTestEnvironment="true"/> 

我需要從Global.asax

找回我的價值isTestEviroment在我使用的那一刻沒有成功:

bool isTestEnvironment = ConfigurationManager.AppSettings.GetValues["isTestEnvironment"]; 

我在做什麼錯在這裏? 注意:我不認爲我的Web.Config文件是正確的,所以如果我沒有正確寫入,請隨時更改它。感謝您的幫助!

+0

在哪種方法你想讀的設置? (會話開始,應用程序開始)? – sll

+0

in Application_Error – GibboK

回答

2

ConfigurationManager.AppSettingsAppSettings配置元素中檢索值,而不是您的自定義部分。

您需要使用:

var section = (HashTable)ConfigurationManager.GetSection("myWebAppSettings"); 
bool isTest = Boolean.Parse(section["isTestEnvironment"].ToString()); 
+0

我如何得到isTestEnvironment? – GibboK

+0

我試着這部分。GetType(isTestEnvironment)==「0」沒有成功。感謝您的幫助! – GibboK

+0

@GibboK - 答案更新。 'GetType'只會獲取屬性的類型,而不是一個值。 – Oded

相關問題