【问题标题】:web.config transform elements not available through System.Configuration.ConfigurationManager通过 System.Configuration.ConfigurationManager 不可用的 web.config 转换元素
【发布时间】:2011-09-30 03:04:47
【问题描述】:

我有一个使用 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"];

知道为什么会这样吗?

【问题讨论】:

    标签: c# visual-studio-2010


    【解决方案1】:

    假设当您从 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 中定义的值(例如,通过发布功能或通过自定义 MSBuild 脚本)。请记住,转换是在您发布时应用的,而不是在您开始运行调试或发布版本时应用的。 web.config 文件中的值始终是受尊重的值。

    【讨论】:

    • 我最初的解决方案是将它们放在 web.config 中,但我认为每次运行项目时都会发生转换。感谢您清除它。
    • 没问题,很高兴能帮上忙。顺便说一句,如果您查看我对这个问题 (stackoverflow.com/questions/3422289/…) 的回答,有一种方法可以使用构建事件在 Visual Studio 中应用转换,但至少可以这么说。我个人不会使用这种方法,但是如果您有兴趣,可以这样做。
    猜你喜欢
    • 2011-07-07
    • 2011-05-26
    • 2010-10-25
    • 1970-01-01
    • 2020-02-06
    • 2011-08-02
    • 1970-01-01
    • 2020-05-19
    • 2012-11-17
    相关资源
    最近更新 更多