8

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

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
Sul Aga
  • 6,142
  • 5
  • 25
  • 37
  • 1
    possible duplicate of [Asp.net 5 publish to IIS, setting ASPNET\_ENV Variables](http://stackoverflow.com/questions/31049152/asp-net-5-publish-to-iis-setting-aspnet-env-variables) – Amit Aug 30 '15 at 22:50
  • Take a look at the following: http://docs.asp.net/en/latest/fundamentals/environments.html -- in particular, pay attention to the NOTE that indicates that web server needs to be restarted for changes in this area to take effect. – David Tansey Aug 30 '15 at 22:54
  • 1
    @Amit it is not because the answer given their doesn't make sense – Sul Aga Aug 30 '15 at 22:59
  • 1
    @DavidTansey there is no mention in the document on how to do it? where to set the variable? – Sul Aga Aug 30 '15 at 23:00

2 Answers2

8

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.

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
8

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>
JOSEFtw
  • 9,781
  • 9
  • 49
  • 67
  • 1
    Please correct the indentation in your markdown so the closing tag (``) is included as code, and the `environmentVariables` subsection is nicely indented. I would have done it myself but it doesn't count as sufficient characters to allow the edit. – Eric Lease Nov 23 '16 at 20:41