Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ASP.NET 5 environment variables on production environment

In Visual Studio 2015 you set the following variable in project properties: ASPNET_ENV. If you set it to development then you can use:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseErrorPage();
    }
 }

IsDevelopment method will check ASPNET_ENV environment variable. Now this is all good on development while you are in Visual Studio 2015. When you publish the web application to IIS on a production server how can you set the value for ASPNET_ENV?

My server is Windows Server 2012

like image 280
Sul Aga Avatar asked Aug 30 '15 22:08

Sul Aga


2 Answers

This is how to set the environment variable on Windows:

  1. On your server, right click 'Computer' or 'My Computer' and click on 'Properties'.
  2. Go to 'Advanced System Settings'.
  3. Click on 'Environment Variables' in the Advanced tab.
  4. Add a new System Variable with the name ASPNET_ENV (RC1) or ASPNETCORE_ENVIRONMENT (RC2, RTM and Above) and a value of Production, Staging, Development or whatever you want.
  5. A reboot of your site may be required.

See also this answer for how to read the environment variable from gulpfile.js.

like image 197
Muhammad Rehan Saeed Avatar answered Oct 06 '22 22:10

Muhammad Rehan Saeed


If you are using IIS to host your application, it's possible to set the environment variables in your web.configfile like this:

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
    <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="QA" />
        <environmentVariable name="AnotherVariable" value="My Value" />
    </environmentVariables>
</aspNetCore>
like image 26
JOSEFtw Avatar answered Oct 06 '22 23:10

JOSEFtw