gpt4 book ai didi

c# - 使用链接文件的 App 或 Web Config 中的 AppSettings

转载 作者:可可西里 更新时间:2023-11-01 08:09:42 25 4
gpt4 key购买 nike

我正在尝试引用 Windows 服务和 ASP.NET MVC 网站之间的一些常见配置设置。我通过在 App.config 或 Web.config(分别)中使用 appSettings 上的文件属性来执行此操作。被引用的文件(名为 common.config)是同一解决方案中单独项目中的链接文件。在两个 项目中,common.config 设置为ContentCopy Always

This stack answer to a similiar question似乎建议至少对于 configSource 这个解决方案是可行的。我不想要 configSource,因为我只希望两个项目中的一些属性是通用的。 更新:我刚刚试过了,configSource 也不起作用。它找不到配置文件。这让我相信 common.config 并不总是被视为具有副本的内容。

示例 App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey" value="1"/>
</appSettings>
</configuration>

示例 Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey2" value="2" />
</appSettings>
</configuration>

示例 common.config(内容 -> 始终复制)

<appSettings>
<add key="CommonKey" value="1" />
</appSettings>

我正在使用 ConfigurationManager/WebConfigurationManager 读取 AppSettings 属性。

知道为什么当 common.config 是一个链接文件时,它的 AppSettings 值没有被使用,而当它没有被链接时它可以正常工作吗?

谢谢!

最佳答案

在 Web.Config 中,您必须添加“bin/”(参见下面的示例)。

默认情况下,web.config 不会复制到 bin 文件夹中,但文件 common.config 复制,因此您必须添加 web.config 的路径。在非 Web 项目中,默认行为是将 App.config 复制到名为 MyProgram.exe.config 的 bin 文件夹中,并且与 common.config 位于同一目录中。

<appSettings file="bin/common.config">

关于c# - 使用链接文件的 App 或 Web Config 中的 AppSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17437340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com