asp.net core 讀取鏈接字符串

從類庫中讀取配置文件須要添加程序集:json

  Install-Package Microsoft.Extensions.Configurationapp

  Install-Package Microsoft.Extensions.Configuration.Jsonui

{
  "ConnectionStrings": {
    "Default": "Data Source = DESKTOP-A8O71KP\\MSSQLSERVER2017;Initial Catalog = test; User ID = sa;Password =123"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

 

using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System.IO;spa

namespace Common
{
    public class ConfigurationManager
    {
        public readonly static IConfiguration Configuration;
        static ConfigurationManager()
        {
       // Configuration
= new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true) .Build(); } public static string conn { get {return Configuration.GetConnectionString("Default"); } } } }

參考:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2code

相關文章
相關標籤/搜索