使用链接文件的App或Web Config中的AppSettings [英] AppSettings in App or Web Config Using a Linked File

查看:97
本文介绍了使用链接文件的App或Web Config中的AppSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to reference some common config settings between a Windows Service and an ASP.NET MVC website. I am doing this by using the file attribute on appSettings in either the App.config or Web.config (respectively). The file (named common.config) that is being referenced is a linked file in a separate project in the same solution. That common.config is set to Content with Copy Always in both projects.

这个类似问题的堆栈答案似乎暗示至少对于configSource而言,此解决方案有效。我不想要configSource,因为我只希望少数几个属性在两个项目中是相同的。 更新:我刚刚尝试过此操作,并且configSource也无法正常工作。它找不到配置文件。这使我相信common.config不会始终被视为具有复制内容。

This stack answer to a similiar question seems to suggest at least for configSource this solution would work. I don't want configSource though as I only want a handful of the properties to be common amongst the two projects. Update: I just tried this, and the configSource also doesn't work. It can't find the config file. This leads me to believe the common.config is not treated as content with copy always.

示例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 属性。

I am using ConfigurationManager / WebConfigurationManager reading from the AppSettings property.

任何想法为何当common.config是链接文件时,不使用AppSettings值,以及何时使用

Any ideas why when the common.config is a linked file, it's AppSettings values are not used and when it is not linked it works as normal?

谢谢!

推荐答案

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

In the Web.Config you must add "bin/" (se example below).

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

By default the web.config is NOT copied into the bin folder but the file common.config is, therefore you must add the path from web.config. In a non-web project the default behavior is that the App.config is copied to the bin folder with name MyProgram.exe.config and is in the same directory as common.config.

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

这篇关于使用链接文件的App或Web Config中的AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆