1

I do MVC app, I have few lines of code I would like to exucute only when I'm developing, and another, similar part of code when the application is deployed. for example I have controller action that sends mail to some address, but I would like that adress is different when I am deploying and testing.

It is hard to change it always, and I often forget to do that.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
Vlado Pandžić
  • 4,879
  • 8
  • 44
  • 75
  • put a flag in your config file and use flag A for local host/dev mac hine and flag B for deployed version. Check the flag during runtime to determine mail destination. – thedrs Jun 02 '13 at 08:54

2 Answers2

6

Visual Studio provides a number of ways to do this, and which method you want to use depends on what exactly you want to do.

For example, you can use build targets to specify which type of build you want, Release and Debug are common, but you can also create others. You can then add #if pre-processor statements in your code to do things depending on which build is selected.

Another method, which sounds like what you want, is to use App Settings in your app or web.config. Then use the build transforms to transform your config based on the type of build (you will see a Web.Debug.config or Web.Release.config for instance. When you publish your site, Visual Studio will automatically apply these tranforms to your config files and change the app settings to whatever you want for that build type.

So, using your example, you would have this in your Web.config:

<appSettings>
    <add key="notifyAddress" value="debug@foo.com" />
</appSettings>

Then, in your Web.Release.config you have this transform:

<appSettings>
    <add key="notifyAddress" value="release@foo.com" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</appSettings>

In your code you have:

string emailAddress = ConfigurationManager.AppSettings["notifyAddress"];

Now, when you publish your site, emailAddress will automatically have release@foo.com.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
1

see if this helps: https://stackoverflow.com/a/4597270/1043824. The recommended way will still be the config file approach.

In Visual Studio 2010, Request.ServerVariables("SERVER_SOFTWARE") will return Nothing (null) for the Development Server, and "Microsoft-IIS/7.5" for my Win7 Pro IIS/VS2010 installation.

Community
  • 1
  • 1
inquisitive
  • 3,549
  • 2
  • 21
  • 47