c# - web.config 转换元素无法通过 System.Configuration.ConfigurationManager 使用

标签 c# visual-studio-2010

我有一个使用 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/

相关文章:

c# - 如何尽可能有效地(内存)保存字符串列表?

c# - 了解适配器模式

visual-studio-2010 - F#: curry 重载/成倍重载问题

c# - 将图像添加到 ImageList 后出现编译错误 RG0000 ("incorrect format")

visual-studio-2010 - 在 vs2010 中,我看不到任何模板,例如空的 asp .net 网站

c# - Entity Framework |错误 : InvalidCastException: The field of type System. Int32 必须是字符串、数组或 ICollection 类型

c# - 从 StackFrame 获取类型 T

c# - 数据 GridView 未在 win 窗体 c# 中显示更新的数据?

c++ - Microsoft 2010 visual c,安装FLTK

c++ - 关于 C++ 中的头文件创建错误