barteloma
barteloma

Reputation: 6875

Change current web.config with custom Nuget package

I am new at creating custom nuget package. I used NuGet Package Explorer and added new Class Library dll to my local nuget repository. Now I can install it to my new projects.

But Sometimes I need install my custom package and change current project web.config file. Add new key or section. Is this possible?

Upvotes: 3

Views: 3941

Answers (1)

Matt Ward
Matt Ward

Reputation: 47987

You can modify the web.config through a web.config.transform file or by using an XML document transform (XDT).

For a .transform you create a web.config.transform file and put in the Content directory of your NuGet package. The web.config.transform file contains the same as a web.config file and it will be applied to the web.config file when you install the NuGet package.

XDTs are more powerful and can do more complicated modifications to the web.config file, such as inserting or removing existing elements. You create a web.config.install.xdt and optionally a web.config.uninstall.xdt file in the Content directory of the NuGet package. These transforms are then run when the NuGet package is installed or uninstalled. An example, taken from the NuGet documentation, is shown below.

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <modules>
            <add name="MyNuModule" type="Sample.MyNuModule" xdt:Transform="Insert" />
        </modules>
    </system.webServer>
</configuration>

The full XDT syntax is documented on the MDSN website

Upvotes: 3

Related Questions