Nuget Package - Web.config.transform Add

I am creating a NuGet package and want it to update web projects in a Web.Config file with specific settings. I use web.config.transform to edit the application web.config file. It works well when I just add appSettings - like this:

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

However, if I try to add to staticContent, it does not seem to change 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 appSettings but not staticContent tags - any ideas?

+4
source share
3 answers

You need to put an empty <staticContent></staticContent> into your web.config and then use xdt: Transform = "Insert" for an 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 paste the value into your conversion file as follows:

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

It's time to find out. Hope this helps.

+3
source

An old question, but if someone lands on it, then the following should work:

In your case, add / update the staticContent element:

This is an alternative solution, so you will not use the .transform file, but rather 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> 

Thus, you do not need to make preliminary updates, just update the package.

Check out this post for XDT support from Nuget 2.6 onwards.

+3
source

Have you tried adding the 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 is good Microsoft documentation there.

If you post the initial markup and want it to look, maybe we could help a little more :)

0
source

Source: https://habr.com/ru/post/1486059/


All Articles