从 asp.net 核心 3.1 中的 app.settings.json 文件获取 ConnectionString 值

Get ConnectionString value from app.settings.json file in asp.net core 3.1

我是运行一个asp.net核心应用3.1版。我必须在哪里读取 appsettings.json 文件中的连接字符串值。我发现了很多与之相关的示例,但是其中 none 对我有用。

下面是使用的代码:

appsettings.json 文件我提供了如下所示的连接字符串值:

"ConnectionStrings": {
      "ConnectionString1": "data source=192.xxx.x.xxx; database=MyDatabase; user id=myuser; password=mypass; Pooling=false; Connection Lifetime=10000;"
    }

在 Startup.cs 文件中,我有如下代码:

public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        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)
        {
            services.AddControllers();
            services.AddSingleton<IConfiguration>(Configuration);
            
        }

现在在控制器中我使用了如下代码:

IConfiguration configure;

public MyAPIController(IConfiguration _config)
        {
            configure = _config;
        }
        
public IActionResult GetSummary([FromBody] ReportParameters rp)
        {
            try
            {
                var connection = configure.GetValue<string>("ConnectionStrings:ConnectionString1");
                var connection1 = configure.GetSection("ConnectionStrings").GetSection("ConnectionString1").Value;
                var connection2 = configure["ConnectionStrings:ConnectionString1"];
                var connection3 = configure.GetConnectionString("ConnectionString1");
                return Ok(SomeValue);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.ToString() + Environment.NewLine + "Error Path: " + Request.Path);
            }
        }

但是上述代码的 none 正在获取连接字符串值。

求推荐。

如果想获取appsettings.js中的ConnectionString,可以使用constructor获取Configuration,并使用_configuration.GetSection("xxx").Value_configuration["xxx"].

代码:

public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IConfiguration _configuration;


        public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            string RedirectUrl = _configuration.GetSection("RedirectUrl").Value;
            return View();
        }
}

结果:

我的代码没有问题。

appsetitngs.json 文件中存在问题。在我看来这不是问题。 我只是更改连接字符串顺序并将其设置为 up 之后我在应用程序中获取连接字符串值。

下面是我改单的地方:

{
  "ConnectionStrings": {
    "ConnectionString1": "data source=192.xxx.x.xxx; database=MyDatabase; user id=myuser; password=myuser; Pooling=false; Connection Lifetime=10000;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

以前我在日志部分之后使用了这个 connectionString 部分。

这可能会帮助那些正在为此奋斗的人。