web.config transformation/Tokenization

(ASP.NET MVC)


Web.config Transformation:
We normally have the different connectionStrings and appSettings in web.config for Development and Prod. Visual studio has the auto transformation feature that transforms the web.release.config to web.config file. so, we can place the dev env  config settings in web.config and prod env config settings in web.release.config. while visual studio publish, it transforms env config settings into the web.config.
To transform/create web.release.config right click on the web.config and select Transform web.config in the context menu
Web.Release.Config:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
 
    <connectionStrings>
      <add name="giriWebDev" connectionString="####" xdt:Locator="Match(name)" xdt:Transform="SetAttributes"/>
    </connectionStrings>
   <appSettings>
     <add key="Env" value="Prod" xdt:Locator="Match(key)" xdt:Transform="SetAttributes"/>
   </appSettings>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  
  </system.web>
</configuration>

As you see, the xdt:Locator & xdt:Transform attirbutes do the transformation of config setting values in web.config during the publish process in visual studio. You can see the transform info in vs output window.

The xdt:locator attribute identifies/searches the config setting in *.config file by its key/name. When it found, the xdt:transform copys the attribute value from web.release.config to web.config.

we can replace the whole connectionStrings setting by xdt:Transform="Replace"

<connectionStrings xdt:Transform="Replace">
      <add name="giriWebDev" connectionString="####"/>
    </connectionStrings>


Ratings


Average Rating: 0.00 by 0 users

11/1/2018
11/1/2018
Download PDF

Comments