Web Config Transform not working

2019-01-13 01:26发布

In a .NET MVC 3.0 Application I have the following configuration in appSettings:

web.config

<appSettings>
<add key="SMTPHost" value="mail.domain.com"/>
    <add key="SMTPUsername" value="user@gmail.com"/>
    <add key="SMTPPort" value="25"/>
    <add key="SMTPPwd" value="mypassword"/>
    <add key="EmailFrom" value="notific@gmail.com"/>
</appSettings>

For debugging, I have the following configuration transform defined:

web.Debug.config

<appSettings>
    <add  key="SMTPPort" value="58" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>

And I run the application in debug mode, but my SMTP port is still taking the value from the web.config, not web.Debug.config.

Can anyone suggest what could be wrong in this configuration?

7条回答
▲ chillily
2楼-- · 2019-01-13 01:37

Recently I had the same problem with a older web.config filed based on .NET Framework 2.0. The solution was simply remove the web.config's namespace (xmlns attibute in configuration root node):

BEFORE: <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

AFTER: <configuration>

查看更多
The star\"
3楼-- · 2019-01-13 01:38

Answering your question isn't simple, because it poses a problem - if you want to transform Web.config with Web.debug.config - where transformation effect should be stored? In Web.config itself? This would overwrite transformation source file! Probably that's why Visual Studio doesn't do transformations during builds.

Previous Matt answer is valid, but you might want to mix them to have generic solution that works when you actually change active solution configuration from debug to release etc. Here's a simple solution:

  1. Create your config transformations for configurations (Debug, Release, etc)
  2. Rename Web.config file to Web.base.config - transformations should automaticly rename accordingly (Web.base.Debug.config, etc)
  3. Add following transformWebConfig.proj XML file to your project folder:
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" DefaultTargets="TransformWebConfig" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="TransformWebConfig">
    <TransformXml Source="Web.base.config" Transform="Web.base.$(CurrentConfig).config" Destination="Web.config" />
  </Target>
</Project>
  1. Navigate to your project properties, choose Build Events and add following content to Post-build event command line:
@if exist "%ProgramFiles(x86)%\MSBuild\12.0\bin" set PATH=%ProgramFiles(x86)%\MSBuild\12.0\bin;%PATH%
msbuild $(ProjectDir)transformWebConfig.proj /t:TransformWebConfig /p:CurrentConfig=$(ConfigurationName) /p:TargetProjectName=$(TargetPath)

Now, when you build your solution, a Web.config file will be created with valid transformations for active configuration.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-13 01:47

The Web.config transforms are only applied as part of a publish operation.

If you wish this to be done as part of an app.config build operation, then you can use the SlowCheetah - XML Transforms Visual Studio plugin:

http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

查看更多
走好不送
5楼-- · 2019-01-13 01:48

Use Octopus Deploy (Community edition is free) and let it transform the web.config for you. Steps:

  1. Set up Octopus to deploy your web application
  2. Make sure your Web.Release.config has the Build Action property set to Content just like your main web.config file.

That's it! Octopus will do the rest without any special config. A default IIS Web Site deploy will do this out of the box:enter image description here

查看更多
【Aperson】
6楼-- · 2019-01-13 01:49

Apparently there's a extension for Visual Studio 2015

https://visualstudiogallery.msdn.microsoft.com/05bb50e3-c971-4613-9379-acae2cfe6f9e

This package enables you to transform your app.config or any other XML file based on the build configuration

查看更多
贪生不怕死
7楼-- · 2019-01-13 01:51

Your immediate question has been answered - the explanation is that the transform is applied on publish, not on build.

However, I think that it does not offer a solution on how to achieve what you want to do.

I have been struggling with this exact problem for a few days now, looking for a way to keep web.config clean and set all keys that vary depending on environment in the respective transform files. My conclusion is that the easiest and most stable solution is to use debug values in the original web.config, that way they are always present when you do debug runs in Visual Studio.

Then create transforms for the different environments you want to publish to - test, integration, production - whatever you have. The now built-in functionality to transform web.config files on publish will suffice for this. No need for SlowCheetah or editing build events nor project files. If you have only web projects that is.

Should you wish to, you could also have the web.debug.config file in your solution, just to keep a separate file with all the values pertaining to the development environment. Be sure to comment in it that the values are not applied when running in Visual Studio though, in case someone else try to use it for that purpose!

查看更多
登录 后发表回答