我有一个使用 web.debug.config 文件转换的 web.config 文件。尝试通过 System.Configuration.ConfigurationManager.AppSettings 访问这些值没有任何结果。
我的 web.config appSettings 是空的。
<configuration>
<appSettings>
</appSettings>
</configuration>
已应用我的 web.debug.config 转换。这是一个示例。
<configuration>
<appSettings>
<add key="CommonURL" value="localhost/mysite/" xdt:Transform="Insert" />
</appSettings>
</configuration>
下面是尝试获取此值的代码,它返回 null。
var cu = System.Configuration.ConfigurationManager.AppSettings["CommonURL"];
知道为什么会这样吗?
最佳答案
假设当您从 Visual Studio 运行时 System.Configuration.ConfigurationManager.AppSettings
没有产生任何结果,那么这是预期的行为。您应该将 CommonURL
appSetting 添加到您的 web.config
文件并将 web.debug.config
中的条目更改为:
<add key="CommonURL" value="localhost/mysite/"
xdt:Transform="Replace" xdt:Locator="Match(key)" />
这些更改将允许您在从 Visual Studio 运行时获取 web.config
中指定的值,并允许将该值替换为 web 中定义的值.debug.config
当您执行转换时(例如,通过 Publish 函数或通过自定义 MSBuild 脚本)。请记住,转换是在您发布时应用的,而不是在您开始运行调试或发布构建时应用的。 web.config 文件中的值始终是受尊重的值。
关于c# - web.config 转换元素无法通过 System.Configuration.ConfigurationManager 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6605388/