【问题标题】:How to read Connection String provided in IIS from ASP.NET Core 2.1?如何从 ASP.NET Core 2.1 读取 IIS 中提供的连接字符串?
【发布时间】:2019-09-17 13:20:09
【问题描述】:

我正在尝试部署到 Windows Server 2016 上的 IIS。SQL Server 连接字符串保存在 IIS 连接字符串中。

如何从 startup.cs 中的 ASP.NET Core 2 应用读取连接字符串?

我试图避免对字符串进行硬编码,也避免将其放入 appsetting.json。

【问题讨论】:

  • “IIS 连接字符串”是什么意思。 - 这是在 web.config 中吗? .net 核心应用 IS appsettings.json. 中连接字符串的正确位置
  • 在 IIS 管理器中,在 ASP.NET 下有“连接字符串”图标。您可以为 SQL Server 添加凭据和连接字符串。
  • 不推荐存储 .net 核心连接字符串的位置 - appsettings.json 是。
  • appsetting.json 将与凭据一起推送到 GitHub。它认为这不是最佳做法。
  • 您使用特定于环境的版本并确保将其排除在您的 repo jerriepelser.com/blog/… 之外

标签: c# sql-server iis asp.net-core


【解决方案1】:

ConfigurationManager 类使您能够访问机器、应用程序和用户配置信息(来自 = Microsoft Docs)。

因此,您可以使用以下方式读取服务器连接字符串:

string connString = System.Configuration.ConfigurationManager.ConnectionStrings["IISConnection"].ConnectionString;

【讨论】:

    猜你喜欢
    • 2017-05-01
    • 1970-01-01
    • 2020-04-17
    • 2019-04-21
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    相关资源
    最近更新 更多