Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuget Package - Web.config.transform Add

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?

like image 942
Deano Avatar asked Mar 23 '23 14:03

Deano


2 Answers

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.

like image 177
KDT Avatar answered Apr 01 '23 19:04

KDT


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.

like image 38
ekenman Avatar answered Apr 01 '23 19:04

ekenman