Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Determine the ASP.NET Core Environment in my Gulpfile.js

I am using ASP.NET Core MVC 6 using Visual Studio 2015. In my gulpfile.js script I want to know if the hosting environment is Development, Staging or Production so that I can add or remove source maps (.map files) and do other things. Is this possible?

UPDATE

Relevant issue on GitHub.

like image 416
Muhammad Rehan Saeed Avatar asked Jul 05 '15 09:07

Muhammad Rehan Saeed


People also ask

How do I check environment variables in .NET Core?

Open project properties by right clicking on the project in the solution explorer and select Properties. This will open properties page. Click on Debug tab and you will see Environment Variables as shown below. You may change the value as per your need.

What is hosting environment in ASP.NET Core?

The hosting environment in ASP.NET Core is used to indicate at runtime on which environment (Development, Staging, or Production) an ASP.NET Core application is running.

Is ASP.NET Core configuration system can be environment specific?

The configuration system in ASP.NET Core is built up of layers of configuration values, compiled from multiple sources. You can load values from JSON files, XML files, environment variables, or you can create a custom provider to load values from pretty much anywhere.

Which of the following is environment variable in ASP.NET Core application?

ASP.NET Core uses the ASPNETCORE_ENVIRONMENT environment variable to determine the current environment. By default, if you run your application without setting this value, it will automatically default to the Production environment.


2 Answers

You can use the ASPNETCORE_ENVIRONMENT (Was formerly ASPNET_ENV in RC1) environment variable to get the environment. This can be done in your gulpfile using process.env.ASPNETCORE_ENVIRONMENT.

If the environment variable does not exist, you can fallback to reading the launchSettings.json file which Visual Studio uses to start your application. If that also does not exist, then fallback to using the Development environment.

I wrote the following JavaScript object to make dealing with the environment in gulpfile.js easier. You can find the full gulpfile.js source code here.

// Read the launchSettings.json file into the launch variable.
var launch = require('./Properties/launchSettings.json');

// Holds information about the hosting environment.
var environment = {
    // The names of the different environments.
    development: "Development",
    staging: "Staging",
    production: "Production",
    // Gets the current hosting environment the application is running under.
    current: function () { 
        return process.env.ASPNETCORE_ENVIRONMENT ||
            (launch && launch.profiles['IIS Express'].environmentVariables.ASPNETCORE_ENVIRONMENT) ||
            this.development;
    },
    // Are we running under the development environment.
    isDevelopment: function () { return this.current() === this.development; },
    // Are we running under the staging environment.
    isStaging: function () { return this.current() === this.staging; },
    // Are we running under the production environment.
    isProduction: function () { return this.current() === this.production; }
};

See this answer for how to set the environment variable.

like image 128
Muhammad Rehan Saeed Avatar answered Oct 30 '22 09:10

Muhammad Rehan Saeed


You would need to set the NODE_ENV environment variable in each environment and then in your gulpfile, read it in using process.env.NODE_ENV.

Have a look at https://stackoverflow.com/a/16979503/672859 for additional details.

like image 30
Patrick Grimard Avatar answered Oct 30 '22 09:10

Patrick Grimard