4

I'm creating a NuGet package and want it to update the web projects Web.Config file with certain settings. I am using the web.config.transform to edit the web.config file of an application. It's working well when I simply add appSettings - like so:

<configuration>
  <appSettings>
    <add key="WebPToolFolder" value ="~/Tools"/>
    <add key="ImagesFolder" value ="~/Content/themes/base/images"/>
  </appSettings>
</configuration>

However, if I try an add to the staticContent it doesn't seem to alter the tags. For example, here is the web.config.transform file:

<configuration>
  <appSettings>
    <add key="WebPToolFolder" value ="~/Tools"/>
    <add key="ImagesFolder" value ="~/Content/themes/base/images"/>
  </appSettings>
<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".webp" mimeType="image/webp" />
    </staticContent>
  </system.webServer>
</configuration>

It updates the appSettings, but not the staticContent tags - any ideas?

Deano
  • 2,805
  • 3
  • 29
  • 42

3 Answers3

4

Old question but if anyone lands on it the following should work:

In your case to add/update the staticContent element:

It's an alternative solution, so you won't use the .transform file, but rather the web.config.install.xdt (and web.config.uninstall.xdt) which I find better:

<?xml version="1.0"?>
  <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!-- some other elements -->
  <staticContent xdt:Transform="InsertIfMissing">
    <mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Locator="Match(fileExtension)" xdt:Transform="InsertIfMissing" />
  </staticContent>
  <!-- some other elements -->
</configuration>

This way you don't need to do any pre-update preperations, just upgrade the package.

Check this post for XDT support from Nuget 2.6 onwards.

KDT
  • 671
  • 1
  • 6
  • 15
3

You need to put an empty <staticContent></staticContent> in your web.config and then use the xdt:Transform="Insert" on the element like this:

Your web.config:

<configuration>
  <appSettings>
     <add key="WebPToolFolder" value ="~/Tools"/>
     <add key="ImagesFolder" value ="~/Content/themes/base/images"/>
  </appSettings>
  <system.webServer>
    <staticContent>
    </staticContent>
  <system.webServer>
</configuration>

And then you can insert a value in your transform file like this:

    <system.webServer>
    <staticContent>
        <mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Transform="Insert"/>
    </staticContent>
</system.webServer>

Took me a while to find out. Hope this helps.

ekenman
  • 995
  • 1
  • 13
  • 29
0

Have you tried adding an xdt:Transform="Replace" attribute to the tags you want to update?

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="WebPToolFolder" value ="~/Tools" xdt:Transform="Replace"/>
    <add key="ImagesFolder" value ="~/Content/themes/base/images" xdt:Transform="Replace"/>
  </appSettings>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".webp" mimeType="image/webp" xdt:Transform="Replace"/>
    </staticContent>
  </system.webServer>
</configuration>

There's some good Microsoft documentation here

If you post the initial markup and what you want it to look like maybe we could help a bit more :)

mkorman
  • 948
  • 2
  • 10
  • 27