[三] ASP.NET Core配置文件

Posted 长不大的大灰狼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[三] ASP.NET Core配置文件相关的知识,希望对你有一定的参考价值。

1、launchsettings.json 文件

此文件仅用于本地开发环境。当启动项目时,会用到此文件中的配置。

2、appsettings.json 文件

在发布和部署应用程序时使用某些独立的设置,它们存储在 appsettings.json 文件中。我们还可以使用不同环境的 appsettings.json 文件。例如,appsettings.Staging.json 用于临时环境。

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "MyKey": " appsettings.json中Mykey的值"
}

3、访问配置信息

若要访问 "Startup " 类中的配置信息, 需要注入框架提供的 IConfiguration服务。IConfiguration 服务是为了从 ASP.NET Core 中的所有各种配置源读取配置信息而设计的。

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 Core配置文件的主要内容,如果未能解决你的问题,请参考以下文章