C# Web API 连接字符串 web.config 未被读取
C# Web API connectionstrings web.config not being read
这是我的第一个问题,所以请保持温和(并就如何改进我的问题提供建设性的批评):)
我正在尝试连接到我的 Web API 项目中的 Oracle 数据库。
这个项目有一个 app.config 和一个 web.config。
我不知道为什么我两者都有,但在设置(空 asp.net 和 Web API)项目时它会随之而来。
这是我连接到数据库并获取连接字符串的代码,这也是我获取 NullReferenceException 的地方:
// create a connection to the database
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString))
并且我包含了适当的使用语句:
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Data;
using Oracle.ManagedDataAccess.Types;
我的web.config文件如下:
<configuration>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<!-- Customize these connection alias settings to connect to Oracle DB -->
<dataSource alias="ALIAS" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=IPOFSERVER)(PORT=PORTOFSERVER))(CONNECT_DATA=(SERVICE_NAME=SERVICENAME)));User Id=USERNAME;Password=PASSWORD;" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
<connectionStrings>
<clear/>
<add name="Oracle" connectionString="Data Source=ALIAS;" providerName="Oracle.ManagedDataAccess.Client"/>
</connectionStrings>
</configuration>
到目前为止我尝试过的事情:
- 改用System.Web.Configuration.WebConfigurationManager
- 检查参考等
- 添加 AppSettings 键和值并尝试从 web.config 文件中读取它们。也 returns null.
除了运行时 NullReferenceException 之外,我没有收到任何错误。
我希望这是足够的信息来提出解决方案。如果没有,请告诉我,我会提供额外的信息。
尝试以下操作。它在 System.Configuration:
之内
var con = ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString;
此外,您可能想尝试在您的配置中不使用 clear/>。
当我的解决方案中有一个控制台应用程序项目用于测试时,我只使用了一个 app.config 文件。我将连接字符串存储在 web.config 中,如下所示:
<connectionStrings>
<add name="NAME" connectionString="Server=Server;Initial Catalog=TABLE_NAME;User ID=ID;Password=PASSWORD;Application Name='WEBSITE'" />
</connectionStrings>
然后我在我的代码中这样引用它:
protected const string ConnectionString = @"NAME";
事实证明这是一个非常简单的修复,通常都是如此。
如前所述,我有一个 app.config 和一个 web.config.
在 web.config 中是 Oracle Client 设置,因此根据之前的经验我认为连接字符串也应该在那里。
但是,在 app.config 中定义它时,它起作用了!
虽然与 2 个配置文件混淆,因为人们希望在 web.config 中定义连接字符串,而不是 app.config.
底线:ConnectionStrings 应该在 app.config.
帮我修好了。
这是我的第一个问题,所以请保持温和(并就如何改进我的问题提供建设性的批评):)
我正在尝试连接到我的 Web API 项目中的 Oracle 数据库。 这个项目有一个 app.config 和一个 web.config。 我不知道为什么我两者都有,但在设置(空 asp.net 和 Web API)项目时它会随之而来。
这是我连接到数据库并获取连接字符串的代码,这也是我获取 NullReferenceException 的地方:
// create a connection to the database
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString))
并且我包含了适当的使用语句:
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Data;
using Oracle.ManagedDataAccess.Types;
我的web.config文件如下:
<configuration>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<!-- Customize these connection alias settings to connect to Oracle DB -->
<dataSource alias="ALIAS" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=IPOFSERVER)(PORT=PORTOFSERVER))(CONNECT_DATA=(SERVICE_NAME=SERVICENAME)));User Id=USERNAME;Password=PASSWORD;" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
<connectionStrings>
<clear/>
<add name="Oracle" connectionString="Data Source=ALIAS;" providerName="Oracle.ManagedDataAccess.Client"/>
</connectionStrings>
</configuration>
到目前为止我尝试过的事情:
- 改用System.Web.Configuration.WebConfigurationManager
- 检查参考等
- 添加 AppSettings 键和值并尝试从 web.config 文件中读取它们。也 returns null.
除了运行时 NullReferenceException 之外,我没有收到任何错误。 我希望这是足够的信息来提出解决方案。如果没有,请告诉我,我会提供额外的信息。
尝试以下操作。它在 System.Configuration:
之内var con = ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString;
此外,您可能想尝试在您的配置中不使用 clear/>。
当我的解决方案中有一个控制台应用程序项目用于测试时,我只使用了一个 app.config 文件。我将连接字符串存储在 web.config 中,如下所示:
<connectionStrings>
<add name="NAME" connectionString="Server=Server;Initial Catalog=TABLE_NAME;User ID=ID;Password=PASSWORD;Application Name='WEBSITE'" />
</connectionStrings>
然后我在我的代码中这样引用它:
protected const string ConnectionString = @"NAME";
事实证明这是一个非常简单的修复,通常都是如此。 如前所述,我有一个 app.config 和一个 web.config.
在 web.config 中是 Oracle Client 设置,因此根据之前的经验我认为连接字符串也应该在那里。 但是,在 app.config 中定义它时,它起作用了! 虽然与 2 个配置文件混淆,因为人们希望在 web.config 中定义连接字符串,而不是 app.config.
底线:ConnectionStrings 应该在 app.config.
帮我修好了。