栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.net core /.net5 如何读取appsettings.json中的DefaultConnection值?

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.net core /.net5 如何读取appsettings.json中的DefaultConnection值?

直接上代码:

配置文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "server=1.1.1.1;port=111;database=test1;SslMode=None;uid=root;pwd=123456;Allow User Variables=true"
  }
}

项目启动代码:
Startup.cs文件中(精简了一部分无关内容)

using Autofac;

using Exceptionless;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using System.Reflection;

using XJ.dDatabase.ConnectionPool;
using XJ.dDatabase.Repository;
using XJ.dDatabase.Service;

namespace XJ.wWeb
{
    public class Startup
    {
        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)
        {
            #region 读取
            var defaultConnection = Configuration.GetConnectionString("DefaultConnection");
            if (!string.IsNullOrEmpty(defaultConnection))
            {
                ConnectionPoolHelper._mySqlConnection = defaultConnection;
            }
            else
            {
                return;
            }
            #endregion

            services
                //.Configure<>() 读取配置暂时就不写了,反正当前能够顺利读取到DefaultConnection得值
                .AddControllersWithViews()
                .AddRazorRuntimeCompilation();
        }


     
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/905920.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号