0

I've created an ASP.Net MVC 6 (ASP 5, ASP vNext RC1) web application which I have deployed (published) on a test server (IIS 7.5) using WebDeploy.

When I run the application locally, the IHostingEnvironment parameter in the startup class is env.EnvironmentName == "Development".

After publishing to the server, env.EnvironemntName == "Production"

Question

How can I change the application to run in the Development (or Test) environment after I have published it on the server?

In other words, I want the env.EnvironmentName == "Development" when the application is running on the IIS.

Additional Information

I have three application settings files: appsettings.json, appsettings.development.json and appsettings.test.json.

Each of these have different connection strings.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128

1 Answers1

1

The most easy way would be setting system environment variable ASPNET_ENV to Development value. See the documentation for additional information.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • It's already set to `Development`. – A-Sharabiani Feb 02 '16 at 20:51
  • 1
    @AliSharabiani: You should verify that `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment` really contains the key with the name `ASPNET_ENV` and the value `Development`. – Oleg Feb 02 '16 at 20:56
  • So on the server, this key didn't exist, I created it manually, and logged the value of the `env.EnvironmentName` to a file, it's still `Production` – A-Sharabiani Feb 02 '16 at 21:08
  • 1
    @AliSharabiani: Sorry, but you should **not create the registry value manually** if you don't want to boot the server. Instead of that you should go to "Advanced" tab of "System Properties" (context menu on This PC) click "Environment Variables..." and then create new /modify existing in "System variables". The difference is: the Control Panel plugin send broadcast message to all windows, which inform about the changes on environment. You can now either create/modify some system Environment Variable or rebbot the server. – Oleg Feb 03 '16 at 05:48
  • I created the environment variable in the System Properties, (both in user variables and system variables), I set the value to `Development`, `Staging` also checked the registry keys, everything is fine. However, when I publish the website, the application is still is running in `Production`. – A-Sharabiani Feb 09 '16 at 20:21
  • @AliSharabiani: It can be that you have to restart IIS server. You can use [ProcessExplore](https://technet.microsoft.com/en-us/sysinternals/bb896653) to examine Environment variables of IIS process. If you see that it has no `ASPNET_ENV` or it has it with old value then you can restart IIS and then verify Environment variables of IIS process once more. – Oleg Feb 10 '16 at 19:34
  • I actually didn't restart the server. In IIS Application Pool I had to check `Load User Profile` to `True` so the environment variables could load. Thanks. – A-Sharabiani Feb 10 '16 at 19:39
  • @AliSharabiani: You are welcome! It's good that you have found the reason. – Oleg Feb 10 '16 at 19:42