Best way to distinguish environment in MVC3?

269 Views Asked by At

I am using MVC3, and I am wondering what is the best way to distinguish your environment? For instance I am thinking of adding a key to appSettings and referencing it, however in MVC3 is there a better way? I am working on 3 environments: Development, Staging, and Production.

Thanks

3

There are 3 best solutions below

0
On BEST ANSWER

I use the configuration manager and define DEBUG, TEST, RELEASE as compile time constants. For configurations I use Web.config Transformation Syntax for Web Application Project Deployment and would highly recommend using them.

For example:

//web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="MyConnectionString"
         connectionString="Data Source=SqlServer\Sql2008;
                           Initial Catalog=MyDB.Dev;
                           Integrated Security=SSPI"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <add key="SomeAppSetting" 
       value="DebugValue"/>
</configuration>

Test transformation:

//web.Test.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="MyConnectionString"
         connectionString="Data Source=SqlServer\Sql2008;
                           Initial Catalog=MyDB.Test;
                           Integrated Security=SSPI"
         providerName="System.Data.SqlClient"
         xdt:Transform="SetAttributes" 
         xdt:Locator="Match(name)"/>
  </connectionStrings>
  <add key="SomeAppSetting" 
       value="TestValue"
       xdt:Transform="SetAttributes" 
       xdt:Locator="Match(key)"/>
</configuration>

When I change my configuration from debug to test and rebuild/deploy my app now uses the transformation update in my web.Test.config. Extremely useful.

You can build different configurations using the Configuration Manager Dialog Box. At anytime you can right click on the web.config and select Add Config Transformations to have Visual Studio 2010 create the transformation config files automagically.

0
On

Web.config is probably a good place, for the MVC UI project. assuming that data layer and service layer are separated you would also have entries in those other projects.

0
On

I would recommend taking a look at web config transformation. Once you have the basis of your web.config setup you would create a transformation for your staging and production environments.

Many good examples on the net if you require more info.