Using web.config to conditionally update an item

I am trying to configure transformation rules for web.config to create or update a connection string. The rules are simple:

  • If a connection string with the given name ("MyDatabase") is present, it should not be touched.

  • If there is no connection string with the given name, insert it.

But I can’t understand if this is possible. If I just specify the β€œadd” element to my web.config.transform, it inserts a connectionString element, even if it already has that name. But if I specify xdt: Transform = "Replace", then it will be replaced. I found a good article on this subject and lists the "Replace, Insert, Delete" scripts. But I need "InsertIfNotExists".

Help is appreciated.

+4
source share
2 answers

Blog Custom Conversions and Merges web.config extensions Merges and MergeBefore converts to insert an element if it is missing, but leave the element alone if it is already present.

To use a custom conversion, you must import the appropriate namespace in your XML conversion:

<xdt:Import assembly="AppHarbor.TransformTester"   namespace="AppHarbor.TransformTester.Transforms"/> 
+2
source

An alternative solution, if you want to update the element in web.config using the configuration conversion, and the element is missing, you can simply add an empty element to web.config and configure your web.Release.config like this:

web.config:

 <system.web> <httpModules> </httpModules> ... 

web.Release.config:

 <system.web> <httpModules> <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" xdt:Transform="Insert" /> <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" xdt:Transform="Insert" /> </httpModules> ... 

Thus, you can combine your new properties into a web configuration when you deploy!

0
source

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


All Articles