asp.net - 如何在 ASP.NET 中将 configSource 与 configBuilders 一起使用?

标签 asp.net visual-studio

我正在关注https://jeffreyfritz.com/2017/11/modern-configuration-for-asp-net-4-7-1-with-configurationbuilders/对于我的 ASP.NET 4.7.2 应用程序,但我们在 Web.config 中有这个:

<connectionStrings configSource="myconfig.config"/>

我希望可以通过将此 block 更改为:使用环境变量暂时设置 myconfig.config 中的值:

<connectionStrings configBuilders="Env" configSource="myconfig.config"/>

但这给了我一个编译器错误:

A section using 'configSource' may contain no other attributes or elements.

这是 myconfig.config 的样子:

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
    <add name="db1" connectionString="conn1" providerName="prov1" />
    <add name="db2" connectionString="conn2" providerName="prov2" />
    <add name="db3" connectionString="conn3" providerName="prov3" />
</connectionStrings>

有什么想法吗?我一直在尝试寻找如何使两者结合起来工作,但没有成功!

最佳答案

对于任何面临此问题的人来说,解决方案是将“configBuilders”属性放在目标文件上,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings configBuilders="Env">
    <add name="db1" connectionString="conn1" providerName="prov1" />
    <add name="db2" connectionString="conn2" providerName="prov2" />
    <add name="db3" connectionString="conn3" providerName="prov3" />
</connectionStrings>

此外,因为之后出现了另一个问题......请确保您使用“configSource”作为属性而不是“file”。

关于asp.net - 如何在 ASP.NET 中将 configSource 与 configBuilders 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67876744/

相关文章:

asp.net - 算术运算导致溢出

asp.net - YouTube 视频集成

asp.net - HttpApplication 事件改变线程

c# - 您的启动项目未引用 Microsoft.EntityFrameworkCore.Design

c++ - 开始 DirectX 11 游戏编程错误

c# - 从 .NET Core 注销 IdentityServer4 让我重新登录

c# - 用于自动为异常生成相关捕获 block 的 Visual Studio 功能?

在没有 Visual Studio 的情况下创建 Windows 服务

c++ - 如何在 Visual Studio 中读取 Unresolved external symbol 错误?

c# - 在 C# 中反序列化不同类型对象的多个 JSON 数组