如何在 Startup 的 ConfigureServices
方法中获取 Development/Staging/production Hosting Environment?
public void ConfigureServices(IServiceCollection services)
{
// Which environment are we running under?
}
ConfigureServices
方法只接受一个IServiceCollection
参数。
最佳答案
您可以在 ConfigureServices 中轻松访问它,只需在 Startup 方法期间将其持久化到一个属性,该方法首先被调用并传入,然后您可以从 ConfigureServices 访问该属性。
public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv)
{
...your code here...
CurrentEnvironment = env;
}
private IWebHostEnvironment CurrentEnvironment{ get; set; }
public void ConfigureServices(IServiceCollection services)
{
string envName = CurrentEnvironment.EnvironmentName;
... your code here...
}
关于c# - 如何在 ConfigureServices 中获取 Development/Staging/production Hosting Environment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548948/