【问题标题】:How to Determine the ASP.NET Core Environment in my Gulpfile.js如何在我的 Gulpfile.js 中确定 ASP.NET Core 环境
【发布时间】:2015-07-05 09:31:23
【问题描述】:

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

更新

GitHub 上的相关问题。

【问题讨论】:

    标签: gulp asp.net-core production-environment visual-studio-2015 asp.net-core-mvc


    【解决方案1】:

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

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

    我编写了以下 JavaScript 对象,以便更轻松地处理 gulpfile.js 中的环境。你可以找到完整的 gulpfile.js 源代码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; }
    };
    

    如何设置环境变量见this回答。

    【讨论】:

    • 这在 RC1 中为空,因此不起作用,仍然没有给我们当前的配置。
    • 您需要在 Windows/Linux/Mac OS 中实际设置 ASPNET_ENV 变量。看到这个:stackoverflow.com/questions/32301840/…
    • 另请注意,此环境变量名称在 RC2 中正在更改。
    • 请注意,您的回答要求我在部署之前设置环境变量,但是因为 VS.net 中的环境变量为项目设置了一次,并且您无法为每个发布目标或构建配置设置它,你不可能做问题中需要做的事情,这是一个主要问题,因为你不能用正确的路径部署角度到你的 API,因为它目前以自动方式重写(使用 gulp.replace 或其他任何东西) api URI。
    • 这个process 变量是从哪里来的...我没有看到它在我的gulpfile.js 中定义
    【解决方案2】:

    您需要在每个环境中设置 NODE_ENV 环境变量,然后在您的 gulpfile 中,使用 process.env.NODE_ENV 读取它。

    查看https://stackoverflow.com/a/16979503/672859 了解更多详情。

    【讨论】:

    • 看来您必须设置节点环境和 ASP.NET 5 环境才能使其正常工作。是否可以基于 ASP.NET 5 托管环境设置节点环境?
    • 我不了解 Visual Studio,因为我从未使用过它,但这取决于您如何运行 gulp 任务。 VS 是否具有运行 gulp 任务的某种嵌入式节点环境?如果是这样,我会查看首选项。如果你的 gulp 任务只是从命令行运行,你必须设置你的操作系统级别的环境变量。
    • 问题是我不想要环境,或者至少我希望能够为每个构建配置(DEBUG、RELEASE 等)设置环境变量,这样当我为我们的演示环境构建时,我知道它将进入演示环境,并且在我们的 Angular 应用程序中相应地设置了 API 的 url。现在我无法部署到特定环境,并且在部署在 Web 浏览器中的 javascript 中可读的重写配置之前。除了从 web.config 或其他方式进行上下文设置之外,我必须无缘无故地使用 mvc。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多