如何在Startup中的ConfigureServices方法中获得开发/登台/生产托管环境?

public void ConfigureServices(IServiceCollection services)
{
    // Which environment are we running under?
}
ConfigureServices方法仅采用单个IServiceCollection参数。

最佳答案

您可以在ConfigureServices中轻松访问它,只需在启动方法期间将其持久保存到属性中,该方法将首先被调用并传递它,然后您可以从ConfigureServices中访问该属性。

public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
    ...your code here...
    CurrentEnvironment = env;
}

private IHostingEnvironment CurrentEnvironment{ get; set; }

public void ConfigureServices(IServiceCollection services)
{
    string envName = CurrentEnvironment.EnvironmentName;
    ... your code here...
}

09-04 09:52