2014-07-05 19 views
0

我有2013轉換從2008年到VS VS 2013System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration

的代碼完全運行在2008年網絡項目,但錯誤在運行時。

從名爲myWebPages的StartUp項目中,代碼將在同一解決方案中的另一個名爲myRemotingService的項目中讀取web.config。

這裏是代碼的一部分:

Dim rootWebConfig1 As System.Configuration.Configuration 

rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/myRemotingService") 

varConnectionString = rootWebConfig1.AppSettings.Settings.Item("ConnectionString").Value 'Error on this line 

但是,當我從myRemotingService複製web.config中myWebPages。和代碼更改爲:

rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/myWebPages") 
varConnectionString = rootWebConfig1.AppSettings.Settings.Item("ConnectionString").Value 

工作正常,但我想在web.config是發生在myRemotingService因爲我要建造更多myWebPages類似的項目。

代碼有什麼問題?

感謝

有錯誤是:對象引用不設置到對象的實例

我也運行一些代碼,我從互聯網

rootWebConfig1 = 
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/myRemotingService") 

Dim appSettings As KeyValueConfigurationCollection = 
rootWebConfig1.AppSettings.Settings 

Console.WriteLine("[appSettings for app at: {0}]", "/myRemotingService") 

Dim key As String 
For Each key In appSettings.AllKeys 
     Console.WriteLine("Name: {0} Value: {1}", _ 
     key, appSettings(key).Value) 
Next key 

但的長度得到appSettings.count是0

+3

「但在運行時2013年出現錯誤。」什麼是錯誤? – Braiam

+0

錯誤是:對象引用未設置爲對象的實例 – user3808458

+0

可能重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a -nullreferenceexception和知識-DO-I-FIX-IT) –

回答

0

我把它放在〜字符。

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/myRemotingService") 

它現在正在工作。

相關問題