简体   繁体   中英

How can I get the ASP.NET Core environment in a static helper method?

I can't use dependency injection, I have static helper class with static methods in my ASP.NET Core project. What is the easiest way to determine that app is running in development environment?

Equivalent of:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment()) // -> this env is what I'm looking for
    {
        app.UseDeveloperExceptionPage();
        app.UseMigrationsEndPoint();
    }
    ...
}

How can I get env manually without DI?

Considering that the Startup is the very first thing that runs on an ASP.NET Core application (after the host is built in the main Program class), there are very simple solutions available:

public class Startup 
{
    public static IWebHostEnvironment Environment { get; }

    public Startup(IWebHostEnvironment environment)
    {
        Environment = environment;
    }
}

Note that the correct solution is, however, not to mix Dependency Injection with static classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM