gpt4 book ai didi

c# - 如何读取 ASP .NET Core Razor Pages 中的连接字符串

转载 作者:行者123 更新时间:2023-12-05 02:08:29 26 4
gpt4 key购买 nike

我不知道如何在 ASP .NET Core 中检索连接字符串。我花了两天时间尝试所有不同的东西,但无法让它发挥作用。

我试过以下方法:

  • 使用 ConfigurationManager.AppSettings 计数返回 0
  • 使用 ConfigurationManager.ConnectionStrings[].ConnectionString - 唯一存在的连接字符串是 .\SQLEXPRESS(必须在 ConnectionStrings[] 中使用索引而不是字符串),指定连接导致对象引用未设置错误
  • 尝试使用 WebConfigurationManager 找不到如何添加它。我试过 System.Web.Configuration、NuGet 包、引用资料。
  • 使用 IConfiguration 和依赖注入(inject) - 我不知道如何读取连接字符串(如何在模型中引用 Controller 并读取“xxx”连接字符串的值)<
ConfigurationManager.ConnectionStrings["SupplierDB"].ConnectionString
// NullReferenceException: Object reference not set to an instance of an object.

ConfigurationManager.ConnectionStrings[0].ConnectionString
// data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

ConfigurationManager.AppSettings.Count.ToString();
// Returns 0

我在 appsettings.json 和 web.config 中指定了相同的连接字符串。有人可以指出我正确的方向吗,因为我目前完全迷失了,因为没有任何效果。

appsettings.json

{
"ConnectionStrings": {
"CustomerDB": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Customer_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
"SupplierDB": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Supplier_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="CustomerDB" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Customer_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>
<add name="SupplierDB" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Supplier_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>
</connectionStrings>
</configuration>

最佳答案

您可以像这样创建一个 AppSettings 类,方法是将您的 appsettings.json 复制到 QuickType :

public class AppSettings
{
public static ConnectionStrings ConnectionStrings { get; set; }
public static Logging Logging { get; set; }
public static string AllowedHosts { get; set; }
}

public class ConnectionStrings
{
public string CustomerDb { get; set; }
public string SupplierDb { get; set; }
}

public class Logging
{
public LogLevel LogLevel { get; set; }
}

public class LogLevel
{
public string Default { get; set; }
public string Microsoft { get; set; }
public string MicrosoftHostingLifetime { get; set; }
}

然后在启动中:

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
Configuration.Get<AppSettings>();
services.AddControllersWithViews();
}

我使用 static 来轻松访问,而不是通过 DI 注入(inject) AppSettings

或者,如果您不想将它们全部放在一个类中,请查看 Options pattern来自微软。

关于c# - 如何读取 ASP .NET Core Razor Pages 中的连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60848234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com