Variables and Web.config

Hello, I’m wondering what is the best way to change things like dataCacheClient in Web.config?

<dataCacheClient>
  <hosts>
    <host name="REPLACE_ME" cachePort="22233" />
  </hosts>
</dataCacheClient>

Hi Alexandr,

Thanks for reaching out.

If all you want to do is change a specific value on your web.config, the easiest way would be using the feature Variables in Files

Hope that helps,
Dalmiro

Hi Dalmiro,

yep it seems that only possible way is to use good old transformations, so if anyone will be interested, you need add Web.Production.config to your project with contents like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
  <dataCacheClient>
    <hosts>
      <host name="192.168.0.20"  xdt:Transform="SetAttributes(name)" />
    </hosts>
  </dataCacheClient>
</configuration>

Then in your deployment process inside Additional transforms put Web.Production.config => Web.config

Note: that Web.Production.config will be run only while deploying to “Production” environment

Only one bad thing, from now on you have two different places with variables

Hi Marchenko,

You could also use the feature “Variables in files” and have:

Your web config like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
  </system.web>
  <dataCacheClient>
    <hosts>
      <host name="#{hostname}"/>
    </hosts>
  </dataCacheClient>
</configuration>

And then a variable called hostname with the value 192.168.0.20. This would take away the need of having another config file for transformations.

Both approaches are ok though. The good old Config Transforms feature one allows you to have more flexibility when modifying the config file with things such as “remove an entire node from the xml” or “add many nodes”, while the Variables in Files works mostly in scenarios where you only need to modify a single value.

Regards,
Dalmiro