How to get the Development/Staging/production Hosting Environment in ConfigureServices

前端 未结 12 1846
时光取名叫无心
时光取名叫无心 2020-12-04 15:51

How do I get the Development/Staging/production Hosting Environment in the ConfigureServices method in Startup?

public void ConfigureServices(IS         


        
12条回答
  •  离开以前
    2020-12-04 16:37

    Starting from ASP.NET Core 3.0, it is much simpler to access the environment variable from both ConfigureServices and Configure.

    Simply inject IWebHostEnvironment into the Startup constructor itself. Like so...

    public class Startup
    {
        public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            Configuration = configuration;
            _env = env;
        }
    
        public IConfiguration Configuration { get; }
        private readonly IWebHostEnvironment _env;
    
        public void ConfigureServices(IServiceCollection services)
        {
            if (_env.IsDevelopment())
            {
                //development
            }
        }
    
        public void Configure(IApplicationBuilder app)
        {
            if (_env.IsDevelopment())
            {
                //development
            }
        }
    }
    

    Reference: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-3.0#inject-iwebhostenvironment-into-the-startup-class

提交回复
热议问题