Question

I am trying to create a web.config installer for my NuGet package using XDT Transforms.

I want to transform web.config file:

<configuration>
    <system.web>
    </system.web>
</configuration>

to look like this:

<configuration>
    <system.web>
        <httpHandlers>
            <add path="*." verb="*" type="CustomHandler" />
        </httpHandlers>
    </system.web>
</configuration>

Here are the transforms I have tried:

Transform #1:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.web>
        <httpHandlers>
            <add path="*." verb="*" type="CustomHandler" xdt:Transform="Insert" />
        </httpHandlers>
    </system.web>
</configuration>

This only works if the target web.config already contains a <httpHandlers /> section.

In the above example (note, there is no <httpHandlers /> section), this results in an error:

No element in the source document matches '/configuration/system.web/httpHandlers/add'

Transform #2:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.web>
        <httpHandlers xdt:Transform="Insert">
            <add path="*." verb="*" type="CustomHandler" />
        </httpHandlers>
    </system.web>
</configuration>

This works as expected in the above example, but given a web.config file with a pre-existing <httpHandlers /> section, that section is duplicated.

Remember, this is for a NuGet package and I can't make assumptions about the state of user's config.

I am new to XDT Transforms, so may have missed something obvious.

Was it helpful?

Solution

Looks like this will do the trick.

New in VS2012, xdt:Transform="InsertIfMissing".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top