⒈添加Json配置文件并将“复制到输出目录”属性设置为“始终复制”

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Warning"
  5. }
  6. },
  7. "AllowedHosts": "*"
  8. }
  1. {
  2. "ConnectionStrings": {
  3. "StudyConnStr": "Data Source=.;Initial Catalog=Study;User ID=sa;Password=admin"
  4. }
  5. }

⒉在Program中加载配置文件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.Logging;
  10.  
  11. namespace EF_SqlServer
  12. {
  13. public class Program
  14. {
  15. public static void Main(string[] args)
  16. {
  17. CreateWebHostBuilder(args).Build().Run();
  18.  
  19. }
  20.  
  21. public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  22. WebHost.CreateDefaultBuilder(args)
  23. .ConfigureAppConfiguration((hostingContext, config) =>
  24. {
  25. config.SetBasePath(Directory.GetCurrentDirectory());
  26. config.AddJsonFile("//Config//dbconfig.json", true, true);
  27. config.AddJsonFile("appsettings.json", true, true);
  28. }).UseStartup<Startup>();
  29. }
  30. }

⒊使用配置文件中的相关属性

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.HttpsPolicy;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12.  
  13. namespace EF_SqlServer
  14. {
  15. public class Startup
  16. {
  17. public Startup(IConfiguration configuration)
  18. {
  19. Configuration = configuration;
  20. }
  21.  
  22. public IConfiguration Configuration { get; }
  23.  
  24. // This method gets called by the runtime. Use this method to add services to the container.
  25. public void ConfigureServices(IServiceCollection services)
  26. {
  27. services.Configure<CookiePolicyOptions>(options =>
  28. {
  29. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  30. options.CheckConsentNeeded = context => true;
  31. options.MinimumSameSitePolicy = SameSiteMode.None;
  32. });
  33. string dbConn = Configuration.GetSection("ConnectionStrings").GetSection("StudyConnStr").Value;
  34. string logDef = Configuration["Logging:LogLevel:Default"];
  35. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  36. }
  37.  
  38. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  39. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  40. {
  41. if (env.IsDevelopment())
  42. {
  43. app.UseDeveloperExceptionPage();
  44. }
  45. else
  46. {
  47. app.UseExceptionHandler("/Home/Error");
  48. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  49. app.UseHsts();
  50. }
  51.  
  52. app.UseHttpsRedirection();
  53. app.UseStaticFiles();
  54. app.UseCookiePolicy();
  55.  
  56. app.UseMvc(routes =>
  57. {
  58. routes.MapRoute(
  59. name: "default",
  60. template: "{controller=Home}/{action=Index}/{id?}");
  61. });
  62. }
  63. }
  64. }

.Net Core Web应用加载读取Json配置文件的更多相关文章

  1. .Net Core控制台应用加载读取Json配置文件

    ⒈添加依赖 Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.FileExtensions Microsoft ...

  2. 【NET Core】.NET Core中读取json配置文件

    在.NET Framework框架下应用配置内容一般都是写在Web.config或者App.config文件中,读取这两个配置文件只需要引用System.Configuration程序集,分别用 Sy ...

  3. [.NET Core] 简单读取 json 配置文件

    简单读取 json 配置文件 背景 目前发现网上的 .NET Core 读取配置文件有点麻烦,自己想搞个简单点的. .NET Core 已经不使用之前的诸如 app.config 和 web.conf ...

  4. 转: web 页面加载速度优化实战-100% 的飞跃提升

    前言 一个网站的加载速度有多重要? 反正我相信之前来 博主网站 的人至少有 50% 在加载完成前关闭了本站. 为啥捏? 看图 首页完整加载时间 8.18s,看来能进来看博主网站的人都是真爱呀,哈哈. ...

  5. web页面加载速度缓慢,如何优化?

    参考博客: https://www.cnblogs.com/xp796/p/5236945.html https://www.cnblogs.com/MarcoHan/p/5295398.html - ...

  6. 【Java Web开发学习】Spring加载外部properties配置文件

    [Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...

  7. EntityFramework Core一劳永逸动态加载模型,我们要知道些什么呢?

    前言 这篇文章源于一位问我的童鞋:在EntityFramework Core中如何动态加载模型呢?在学习EntityFramwork时关于这个问题已有对应园友给出答案,故没有过多研究,虽然最后解决了这 ...

  8. WEB系统启动时加载Log4j的配置文件

    如何在系统启动的时候加载log4j的配置文件呢? 1.自定义监听类并且继承“ServletContextListener”接口: package cn.ibeans.common; import ja ...

  9. JAVA Web.xml 加载顺序

    web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> ...

随机推荐

  1. 「美团 CodeM 初赛 Round A」试题泛做

    最长树链 树形DP.我们发现gcd是多少其实并不重要,只要不是1就好了,此外只要有一个公共的质数就好了.计f[i][j]表示i子树内含有j因子的最长链是多少.因为一个数的不同的质因子个数是log级别的 ...

  2. vue中render: h => h(App)的详细解释

    2018年06月20日 10:54:32 H-L 阅读数 5369   render: h => h(App) 是下面内容的缩写:   render: function (createEleme ...

  3. ACM之路(19)—— 主席树初探

    长春赛的 I 题是主席树,现在稍微的学了一点主席树,也就算入了个门吧= = 简单的来说主席树就是每个节点上面都是一棵线段树,但是这么多线段树会MLE吧?其实我们解决的办法就是有重复的节点给他利用起来, ...

  4. Android学习_7/26

    四种基本布局 1. 线性布局(LinearLayout) android:layout_gravity:指定控件在布局中的对齐方式 android:gravity:指定文字在控件中的对齐方式 andr ...

  5. 谷歌protocolbuff使用说明步骤

    Protocolbuff 目录 1       Protocolbuff定义和作用... 1 2       Protocolbuff的使用步骤... 1 3       .proto编写格式... ...

  6. [题解] [JLOI2013] 卡牌游戏

    题面 题解 概率dp, 应该做得还是比较少的 设\(f[i][j]\)为该圈有\(i\)人时, 第\(j\)个人最后胜利的概率 枚举选择第几张卡牌, 设其值为\(card[k]\), 那么被淘汰的则是 ...

  7. flask-数据库 进阶

    1. 级联操作 Cascade意为“级联操作”,就是在操作一个对象的同时,对相关的对象也执行某些操作.我们通过一个Post模型和Comment模型来演示级联操作,分别表示文章(帖子)和评论,两者为一对 ...

  8. 关于mysql创建数据库,基字符集 和 数据库排序规则 的对比选择

    1.一般选择utf8.下面介绍一下utf8与utfmb4的区别. utf8mb4兼容utf8,且比utf8能表示更多的字符.至于什么时候用,看你的做什么项目了,unicode编码区从1 - 126就属 ...

  9. link和Import区别

    本篇文章重点: link引用CSS时,在页面载入时同时加载: import需要页面完全载入后加载: link支持使用javascript控制DOM去改变样式,而Import不支持 下面待我娓娓道来: ...

  10. ssh 远程登录错误

    错误信息: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE HOST IDENTIFICATI ...