Tyson Nero
Tyson Nero

Reputation: 2078

.NET Core project.json commands - Set ASPNETCORE_ENVIRONMENT

From command line, if I run set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run, my Hosting environment is set to Development.

However, if I add the same line as a command to my project.json file, the hosting environment for the watcher is always production:

"commands": {
  "watch": "set ASPNETCORE_ENVIRONMENT=Development && dotnet watch run"
},

Are there any arguments I can pass to dotnet run to make the hosting environment Development? I do need this to work as a command.

Upvotes: 2

Views: 1183

Answers (2)

Tyson Nero
Tyson Nero

Reputation: 2078

I finally figured this out!

The trick was to right click on the Project, go to Properties then select the Debug tab. Next, under Profile, I selected the name of the command defined in my projects.json: "watch". Once selected, I clicked Add by Environment Variables, and added the Name/Value pair ASPNETCORE_ENVIRONMENT and Development.

enter image description here

What this actually does is is upload the launchSettings.json file under Properties in Solution Explorer. This file could also just be edited manually. The output looks something like this:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56846/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "LendingTree.CreditCards.Web": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Watcher Development": {
      "commandName": "Watcher Development",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Upvotes: 0

Kiran
Kiran

Reputation: 57969

You can add the Microsoft.Extensions.Configuration.CommandLine package which reads the configuration from command line:

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddCommandLine(args)
            .Build();

        var host = new WebHostBuilder()
            .UseEnvironment(config["ASPNETCORE_ENVIRONMENT"])
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

With dotnet run you could do like below:

dotnet run --ASPNETCORE_ENVIRONMENT Development

You should be able to do something similar with dotnet watch run too.

Upvotes: 3

Related Questions