当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET Core appsettings.json 文件

ASP.NET Core appsettings.json 文件

2019年09月03日  | 移动技术网IT编程  | 我要评论

临沂天气预报,张幼仪,殖民星际

asp.net core appsettings.json 文件

在本节中,我们将讨论 asp.net core 项目中appsettings.json文件的重要性。

在以前的 asp.net 版本中,我们将应用程序配置设置(例如数据库连接字符串)存储在web.config文件中。 在 asp.net core 中, 应用程序配置设置可以来自以下不同的配置源。

  • 文件(appsettings.json, appsettings..json) environment环境不同,托管在对应环境。
  • user secrets (用户机密)
  • environment variables (环境变量)
  • command-line arguments (命令行参数)

appsettings.json文件: 我们的项目是通过 asp.net core 预制的"空"模板创建的,所以我们的项目中已经有一个 appsettings.json 的文件了。 我们可以对文件进行如下修改,补充一个mykey的键值对:

{
  "logging": {
    "loglevel": {
      "default": "warning"
    }
  },
  "allowedhosts": "*",
  "mykey": " appsettings.json中mykey的值"
}

访问配置信息

若要访问 "startup " 类中的配置信息, 请注入框架提供的 iconfiguration服务。startup类位于 startup. cs 文件中。

public class startup
{
    private iconfiguration _configuration;

    //注意,我们在这里使用了依赖注入
    public startup(iconfiguration configuration)
    {
        _configuration = configuration;
    }

    public void configureservices(iservicecollection services)
    {
    }

    public void configure(iapplicationbuilder app, ihostingenvironment env)
    {
        if (env.isdevelopment())
        {
            app.usedeveloperexceptionpage();
        }

        app.run(async (context) =>
        {
            await context.response.writeasync(_configuration["mykey"]);
        });
    }
}

依赖注入

在以前版本的 asp.net 中,依赖注入是可选的,要配置它,我们必须使用像 ninject,autofac、castle windsor 等第三方框架。

在 asp. net core 中, 依赖注入是不可或缺的一部分。依赖注入能使我们能够创建低耦合、可扩展且易于测试的系统。

我们将在即将推出的视频中详细讨论依赖注入,尽情期待。

asp.net core iconfiguration 服务

  • iconfiguration 服务是为了从 asp.net core 中的所有各种配置源读取配置信息而设计的。
  • 如果在多个配置源中具有相同密钥名称的配置设置,简单来说就是重名了,则后面的配置源将覆盖先前的配置源  。
  • 静态类webhostcreatedefaultbuilder()方法在应用程序启动时会自动去调用,按特定顺序读取配置源。
  • 要查看配置源的读取顺序,请查看以下链接上的configureappconfiguration()方法 https://github.com/aspnet/metapackages/blob/release/2.2/src/microsoft.aspnetcore/webhost.cs

检查文件后,您将看到,以下是读取各种配置源的默认顺序

  1. appsettings.json,
  2. appsettings..json
  3. 用户机密
  4. 环境变量 5.命令行参数

如果您想要改变他们的调用顺序,甚至往里面添加属于自己的自定义配置信息,我们将在后面的课程中讨论如何自定义配置源。

小结

所以翻源代码也没有那么可怕嘛

///<summary>
        ///initializes a new instance of the <see cref="webhostbuilder"/> class with pre-configured defaults using typed startup.
        ///</summary>
        ///<remarks>
        ///  the following defaults are applied to the returned <see cref="webhostbuilder"/>:
        ///    use kestrel as the web server and configure it using the application's configuration providers,
        ///    set the <see cref="ihostingenvironment.contentrootpath"/> to the result of <see cref="directory.getcurrentdirectory()"/>,
        ///    load <see cref="iconfiguration"/> from 'appsettings.json' and 'appsettings.[<see cref="ihostingenvironment.environmentname"/>].json',
        ///    load <see cref="iconfiguration"/> from user secrets when <see cref="ihostingenvironment.environmentname"/> is 'development' using the entry assembly,
        ///    load <see cref="iconfiguration"/> from environment variables,
        ///    load <see cref="iconfiguration"/> from supplied command line args,
        ///    configure the <see cref="iloggerfactory"/> to log to the console and debug output,
        ///    enable iis integration.
        ///</remarks>
        ///<typeparam name ="tstartup">the type containing the startup methods for the application.</typeparam>
        ///<param name="args">the command line args.</param>
        ///<returns>the initialized <see cref="iwebhostbuilder"/>.</returns>
        public static iwebhostbuilder createdefaultbuilder<tstartup>(string[] args) where tstartup : class =>
            createdefaultbuilder(args).usestartup<tstartup>();

 

欢迎添加个人微信号:like若所思。

欢迎关注我的公众号,不仅为你推荐最新的博文,还有更多惊喜和资源在等着你!一起学习共同进步!


 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网