2013-08-28 23 views
10

我想獲得一個logFilePath值,我通過硬編碼給appSettings。我試圖通過如何從MVC4中的web.config獲取字符串值

System.Configuration.Configuration rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null); 
System.Configuration.KeyValueConfigurationElement customSetting = rootWebConfig1.AppSettings.Settings["azureLogUrl"]; 
string pathValue = customSetting.Value; 

達到關鍵值,但我得到空引用異常。 如何從web.config文件獲取值?

回答

43

用途:

string pathValue = ConfigurationManager.AppSettings["azureLogUrl"]; 

你不需要施放此字符串,並檢查空值,因爲文件規定:從Web.config文件的元素的appSettings閱讀

值爲 總是輸入String。如果 Web.config文件中不存在指定的密鑰,則不會發生錯誤。相反,返回的空字符串是 。

+1

需要使用System.Configuration增加; – redwards510

2

你可以從web.config中這樣的值:

string pathValue = WebConfigurationManager.AppSettings["azureLogUrl"].ToString(); 
相關問題