web.config的连接字符串未通过configurationManager.connectionstrings读取[test; test']。connectionsTring

发布于 2025-02-06 20:30:45 字数 819 浏览 0 评论 0 原文

我的web.config文件中有一个连接字符串:

<configuration>
    <connectionStrings>
        <add name="Test" connectionString="MyConnString"/>
    </connectionStrings>
</configuration>

并且正在尝试在我的代码中访问它:

string connectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;

为什么我会收到此错误?

nullReferenceException:对象引用未设置为一个实例 对象。

configurationManager.connectionstrings [“ test”] 是无效的,但我不知道原因。

I am using ASP.NET Core 3.1

I installed System.Configuration.Configuration version 6.0.0 using NuGet.

我使用代码文件顶部的语句具有正确的正确信息:使用System.Configuration;

I have a connection string in my Web.Config file:

<configuration>
    <connectionStrings>
        <add name="Test" connectionString="MyConnString"/>
    </connectionStrings>
</configuration>

And am trying to access it in my code like this:

string connectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;

Why am I receiving this error?

NullReferenceException: Object reference not set to an instance of an
object.

ConfigurationManager.ConnectionStrings["Test"] is NULL but I can't figure out why.

I am using ASP.NET Core 3.1

I installed System.Configuration.Configuration version 6.0.0 using NuGet.

I have the correct using statement at the top of my code file: using System.Configuration;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2025-02-13 20:30:45

asp.net core不再使用 global.asax web.config 文件
使用ASP.NET的先前版本。

web.config 文件也已替换为ASP.NET Core。
现在可以配置配置本身,作为应用程序的一部分
startup.cs 中描述的启动过程。配置仍然可以
使用XML文件,但通常会放置ASP.NET核心项目
JSON-Formatted文件中的配置值,例如
AppSettings.json

阅读以下文章:迁移配置为asp。 NET Core

So, you can rename the web.config to app.config.

但是更好的解决方案是更新应用程序以使用JSON-Formatted文件,例如 AppSettings.json 。有关更多信息,请阅读以下文章:

ASP.NET Core no longer uses the Global.asax and web.config files that
previous versions of ASP.NET utilized.

The web.config file has also been replaced in ASP.NET Core.
Configuration itself can now be configured, as part of the application
startup procedure described in Startup.cs. Configuration can still
utilize XML files, but typically ASP.NET Core projects will place
configuration values in a JSON-formatted file, such as
appsettings.json.

Read the following article: Migrate configuration to ASP.NET Core

So, you can rename the web.config to app.config.

But better solution is to update application to use JSON-formatted file, such as appsettings.json. For more information read the following article: Configuration in ASP.NET Core

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文