How to get the ASP.NET Core launch environment on MSBuild or NPM scripts?

Viewed 1893

ASP.NET Core environments are specified by setting an environment variable named ASPNETCORE_ENVIRONMENT. Visual Studio 2017 uses a launchSettings.json file and Visual Studio Code uses a launch.json file to pass per-session environment variables for debugging.

This way, one can define different profiles for development, staging and production using Visual Studio 2017 and debug without setting a system-wide environment variable.

I want to know if I can incorporate this environment set by launchSettings.json/launch.json into my build process.

  1. MSBuild (.csproj)

    $(ASPNETCORE_ENVIRONMENT) returns empty unless I set a global environment variable. Since building the project is done before launching it, I doubt I can get the environment specified in the launch configuration file.

  2. NPM (package.json)

    I have multiple scripts defined in my package.json file and I would love to pass the current environment to my build scripts, although the previous point applies.

I was using DEV/PROD constants defined in the .csproj file and executing the relevant NPM scripts via <Exec Condition="..." Command="..." /> elements before .NET Core. This way compiling the project with the PROD constant made everything ready for publishing.

I wonder what are the current best practices for such things.

0 Answers
Related