Search code examples
.netnugetweb-config-transform

How to add a target application name into a config transform when installing NuGet package


NuGet allows package developer to transform the config and code files in a target project as per this document reference.

Basically, for .config xml-based files merge transfrom and now a common .xdt transforms are available.

I am currently using the following as a web.config.transform:

<configuration>
    <connectionStrings>
        <add name="MembershipConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-membership-hardcodedstring.mdf;Initial Catalog=aspnet-membership-hardcodedstring;Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

See where I have {hardcodedstring} as a part of my database filename? I'd like this one to be a target application namespace or something relevant to a target application.

Can such customization be done to acheieve my goal? If yes, please explaing how.


Solution

  • NuGet XDT supports the use of ProjectProperties in the transform file, such as:

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <appSettings xdt:Transform="Insert">
         <add key="RootNameSpace" value="$rootnamespace$" />
         <add key="AssemblyName" value="$AssemblyName$" />
         <add key="DefaultNameSpace" value="$DefaultNameSpace$" />
         <add key="FullPath" value="$FullPath$" />
         <add key="FileName" value="$filename$" />
         <add key="ActiveConfigurationSettings " value="$ActiveConfigurationSettings$" />
         <add key="AbsoluteProjectDirectory" value="$AbsoluteProjectDirectory$" />
        </appSettings>
    </configuration>