我有一个网络项目从VS2008转换到VS2013。
该代码在2008年运行良好,但在运行时在2013上出现错误。
从名为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但是当我将web.config从myRemotingService复制到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
发布于 2014-07-06 16:46:40
我通过将~字符放入来获得它。
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/myRemotingService")它现在起作用了。
https://stackoverflow.com/questions/24590655
复制相似问题