问题描述
我在 Visual Studio 2015 中使用 ASP.NET Core MVC 6.在我的 gulpfile.js 脚本中,我想知道托管环境是开发、暂存还是生产环境,以便我可以添加或删除源映射(.map 文件) 并做其他事情.这可能吗?
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?
更新
GitHub 上的相关问题.
推荐答案
您可以使用ASPNETCORE_ENVIRONMENT
(在RC1中以前是ASPNET_ENV
)环境变量来获取环境.这可以使用 process.env.ASPNETCORE_ENVIRONMENT
在您的 gulpfile 中完成.
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
.
如果环境变量不存在,您可以回退到读取 Visual Studio 用来启动应用程序的 launchSettings.json
文件.如果这也不存在,则回退到使用开发环境.
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.
我编写了以下 JavaScript 对象,以便更轻松地处理 gulpfile.js 中的环境.你可以找到完整的 gulpfile.js 源代码 这里.
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.
这篇关于如何在我的 Gulpfile.js 中确定 ASP.NET Core 环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!