2

We are trying to setup an ASP.NET MVC 6 web application. In previous versions of ASP.NET we always used transformations for the Web.config. We did this by creating, for example, Web.Production.config. In this case Production matched with the name of the build configuration of the project/solution and was applied as a transformation to the original Web.config.

In the new version, configurations are done using JSON. However, such automatic transformations do not exist anymore. What is the best way to accomplish the same in ASP.NET MVC 6?

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
  • read http://docs.asp.net/en/latest/fundamentals/environments.html – VeNoMiS Dec 21 '15 at 13:28
  • @VeNoMiS, I saw that, but that only works if one machine works with one build environment. It is not an easy way to, for example, test a staging build on your development machine. Additionally, chaging that value as well as your build configuration to run a different configuration on your local machine is duplicate work that should not be necessary. – Patrick Kostjens Dec 21 '15 at 13:29
  • an approch similar to the old one is defined [here](http://stackoverflow.com/questions/26468521/how-to-handle-debug-release-config-transformations-in-asp-net-vnext) but i still don't get your point. – VeNoMiS Dec 21 '15 at 13:43
  • @VeNoMiS, this still requires that you set an environment variable after changing your build configuration. I am hoping for a better solution than that. About the use case: I do not think it is odd to use multiple build configurations on your development machine. Or do I not get your point? However, the question seems to be very similar, but I am hoping for an easier solution. – Patrick Kostjens Dec 21 '15 at 13:48
  • Well No. Each configuration use the same variable but with different values. Release use ASPNET_ENV = Release. Staging ASPNET_ENV = Staging And so on... – VeNoMiS Dec 21 '15 at 14:04
  • I get that, but basically I want that ASPNET_ENV variable to be dependant on the build configuration and not on the machine the application is being executed on. – Patrick Kostjens Dec 21 '15 at 14:08

1 Answers1

3

There is no more "automatic transformations".

The way the configuration work is a bit like "extending" properties in jQuery.

If two parameters in 2 different configuration are made (appsettings.json and appsettings.Production.json) then the latter will take precedence

So let's see if we can solve your issue.

Here's what my appsettings.json would look like:

{
   "myValue" : 1
}

And here's what appsettings.Production.json would look like:

{
   "myValue" : 3
}

The first file would be included in your build and would automatically be used by .NET for getting your configuration. So how does it pick up the "Production" one?

Well the answer can be found in the Startup.cs constructor:

 // Set up configuration sources.
 var builder = new ConfigurationBuilder()
     .AddJsonFile("appsettings.json")
     .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

Of course, you could potentially pick any variables for your configuration file or load them up directly from the Environment Variables or any other source really.

Side note

The name "environment variable" seem to be that it must set globally for everyone but there's nothing preventing you to set them only for the current process by setting them inline before invoking your script.

So dnx web will start your application automatically in production on your machine but starting it with Visual Studio (who will automatically set the environment to Development) will start it in DEV mode.

Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
  • The EnvironmentName does not depend on the build configuration, right? Do I understand you correctly if the buid configuration does not really matter in this situation, but only the environment variable which is used runtime? – Patrick Kostjens Dec 21 '15 at 14:42
  • 100% correct. The build configuration is irrelevant. You can change the configuration at runtime. – Maxime Rouiller Dec 21 '15 at 14:59
  • So I guess any sort of configuration switching based on the build configuration is nog longer possible? – Patrick Kostjens Dec 21 '15 at 15:04
  • If it's related to compile-time? I guess it is possible. But configuring your application at compile time? That is so 2009. ;-) Now we configure the environment name on the machine and we load that configuration. :) The default by the way will always be `Production` if none is specified. Everything else? Up to you. :) – Maxime Rouiller Dec 21 '15 at 15:05
  • Yes that is what I meant. I guess we are just going to have to change that process a bit, since it is not really possible anymore. – Patrick Kostjens Dec 21 '15 at 15:20
  • My pleasure to be of any kind of help. :) – Maxime Rouiller Dec 21 '15 at 15:21
  • This no longer works, {env.EnvironmentName} does not get evaluated in the latest version. – user3138311 Apr 13 '16 at 15:34
  • To be fair, this code was for RC1. In RC2, the configuration builder require that the basePath be given before doing "AddJson". – Maxime Rouiller Apr 13 '16 at 16:30
  • Is there a way to "bind" different environments to different publish profiles? Or how can I auto change my env on publishing. Currently (in version 1.0.0) the publish is always for "Production" env... – Vladislav Sep 08 '16 at 07:40
  • How can I specify different environments i.e. `env.Environment` values as a config setting (yes, at compile time). This is necassary for me because I have two different environments hosted on the same physical server, under two different sites. – James Wierzba Mar 28 '17 at 17:16