如何在我的Gulpfile.js中确定ASP.NET核心环境 [英] How to Determine the ASP.NET Core Environment in my Gulpfile.js

查看:236
本文介绍了如何在我的Gulpfile.js中确定ASP.NET核心环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Core MVC 6使用Visual Studio 2015.在我的gulpfile.js脚本中,我想知道宿主环境是开发,暂存还是生产,以便我可以添加或删除源映射(.map文件)并做其他事情。这可能吗?

更新



GitHub

解决方案

您可以使用 ASPNETCORE_ENVIRONMENT (以前是RC1中的 ASPNET_ENV )环境变量来获取环境。这可以使用 process.env.ASPNETCORE_ENVIRONMENT

如果环境变量不存在,您可以回退阅读Visual Studio用来启动应用程序的 launchSettings.json 文件。如果这还不存在,那么回退到使用开发环境。



我编写了以下JavaScript对象,以便更轻松地处理gulpfile.js中的环境。你可以找到完整的gulpfile.js源代码

  //将launchSettings.json文件读入启动变量。 
var launch = require('./ Properties / launchSettings.json');

//保存有关宿主环境的信息。
var environment = {
//不同环境的名称。
开发:开发,
分段:分段,
生产:生产,
//获取应用程序正在运行的当前宿主环境。
current:function(){
return process.env.ASPNETCORE_ENVIRONMENT ||
(launch& launch.profiles ['IIS Express']。environmentVariables.ASPNETCORE_ENVIRONMENT)||
this.development;
},
//我们是否在开发环境下运行。
isDevelopment:function(){return this.current()=== this.development; },
//我们是在分段环境下运行的。
isStaging:function(){return this.current()=== this.staging; },
//我们是否在生产环境下运行。
isProduction:function(){return this.current()=== this.production; }
};

请参阅 回答如何设置环境变量。


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.

解决方案

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.

这篇关于如何在我的Gulpfile.js中确定ASP.NET核心环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆