user8280126
user8280126

Reputation:

MVC Core Set IHostingEnvironment to Development

How Do I manually set the IhostingEnvironment env to development? I would like to use C# code to do this, not command line.

Thanks,

   public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder();

        if (env.IsDevelopment())
        {

Upvotes: 2

Views: 679

Answers (1)

user1672994
user1672994

Reputation: 10839

You can set the env.EnvironmentName to "Development" in the Startup method.

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder();
    env.EnvironmentName = "Development";  // <- Set the EnvironmentName to "Development"

    if (env.IsDevelopment())
    {

And if you see the implementation of IsDevelopment method at here (github repo), you will observe that it works based on string compare operation for EnvironmentName.

    public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment)
    {
        if (hostingEnvironment == null)
        {
            throw new ArgumentNullException(nameof(hostingEnvironment));
        }

        return hostingEnvironment.IsEnvironment(EnvironmentName.Development);
    }


    public static bool IsEnvironment(
        this IHostingEnvironment hostingEnvironment,
        string environmentName)
    {
        if (hostingEnvironment == null)
        {
            throw new ArgumentNullException(nameof(hostingEnvironment));
        }

        return string.Equals(
            hostingEnvironment.EnvironmentName,
            environmentName,
            StringComparison.OrdinalIgnoreCase);
    }

Upvotes: 1

Related Questions