首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration
EN

Stack Overflow用户
提问于 2014-07-06 05:18:09
回答 1查看 2.6K关注 0票数 0

我有一个网络项目从VS2008转换到VS2013。

该代码在2008年运行良好,但在运行时在2013上出现错误。

从名为myWebPages的StartUp项目中,代码将读取同一解决方案中名为myRemotingService的不同项目中的web.config。

以下是代码的一部分:

代码语言:javascript
复制
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时。并将代码更改为:

代码语言:javascript
复制
rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/myWebPages")
varConnectionString = rootWebConfig1.AppSettings.Settings.Item("ConnectionString").Value

运行良好,但我希望将web.config放在myRemotingService中,因为我将构建更多类似的myWebPages项目。

代码有什么问题?

谢谢

错误是:对象引用未设置为对象的实例

我还运行一些从互联网上获得的代码。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2014-07-06 16:46:40

我通过将~字符放入来获得它。

代码语言:javascript
复制
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/myRemotingService")

它现在起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24590655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档