urza.cc
urza.cc

Reputation: 263

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?

Upvotes: 0

Views: 1670

Answers (1)

Camilo Terevinto
Camilo Terevinto

Reputation: 32063

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.

Upvotes: 1

Related Questions