Czy jest jakiś sposób na "zastąpienie lub wstawienie" za pomocą web.Config transformation?
Używam sieci.transformacja config opisana w poniższym poście w celu wygenerowania configów dla różnych środowisk.
Http://vishaljoshi.blogspot.com/2009/03/web-deployment-webconfig-transformation_23.html
Mogę wykonać transformację" Replace " dopasowując na kluczu, np.
<add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
I mogę robić "Wstawki" np.
<add key="UseLivePaymentService" value="true" xdt:Transform="Insert" />
Ale to, co chciałbym naprawdę znaleźć przydatne jest transformacja zastępcza, ponieważ nie zawsze mogę polegać na oryginalny plik konfiguracyjny mający / Nie mający określonego klucza.
Czy jest jakiś sposób, aby to zrobić?5 answers
Znalazłem tanie obejście. To nie jest ładne i nie będzie działać bardzo dobrze, jeśli masz wiele elementów, które muszą być "wymienić lub wstawić".
Wykonaj "Remove", a następnie "InsertAfter|InsertBefore".
Na przykład,
<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertAfter(/configuration/system.web/authentication)">
<deny users="?"/>
<allow users="*"/>
</authorization>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-12 16:44:32
W połączeniu z xdt:Transform="Remove"
Użyj xdt:Transform="InsertIfMissing"
w VS2012.
<authorization xdt:Transform="Remove" />
<authorization xdt:Transform="InsertIfMissing">
<deny users="?"/>
<allow users="*"/>
</authorization>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-25 11:03:56
Użyj transformacji InsertIfMissing
, aby upewnić się, że appSetting istnieje.
Następnie użyj transformacji Replace
, aby ustawić jej wartość.
<appSettings>
<add key="Environment" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
<add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>
Można również użyć transformacji SetAttributes
zamiast Replace
. Różnica polega na tym, że SetAttributes
nie dotyka węzłów potomnych.
<appSettings>
<add key="UseLivePaymentService" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" />
<add key="UseLivePaymentService" value="true" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
Techniki te są znacznie lepsze niż remove+insert, ponieważ istniejące węzły nie są przenoszone na dno węzła nadrzędnego. Nowe węzły są dołączane na końcu. Istniejące węzły pozostają tam, gdzie znajdują się w źródle plik.
Ta odpowiedź dotyczy tylko nowszych wersji programu Visual Studio (2012 lub nowszych).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-06 07:26:50
Lepszą metodą było wstawianie elementu tylko wtedy, gdy nie istnieje, ponieważ ustawiam tylko pewne atrybuty. Usunięcie elementu odrzuciłoby wszelkie inne atrybuty głównego elementu, gdyby istniały.
Przykład: www.config (without element)
<serviceBehaviors>
<behavior name="Wcf.ServiceImplementation.AllDigitalService_Behavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
Www.config (with element)
<serviceBehaviors>
<behavior name="Wcf.ServiceImplementation.AllDigitalService_Behavior">
<serviceDebug httpsHelpPageEnabled="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
Używając lokalizatora z wyrażeniem XPath, dodaję węzeł, jeśli nie istnieje, a następnie ustawiam atrybut:
<serviceDebug xdt:Transform="Insert"
xdt:Locator="XPath(/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior[not(serviceDebug)])" />
<serviceDebug includeExceptionDetailInFaults="true" xdt:Transform="SetAttributes" />
Obie strony wynikowe.pliki konfiguracyjne mają includeExceptionDetailInFaults= "true", a drugi zachowuje atrybut httpsHelpPageEnabled, gdzie metoda remove/insert nie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-19 05:28:07
Poniżej tworzy nowy klucz jest ten sam klucz nie jest obecny. jeśli jest obecny, to po prostu zastępuje istniejący.
<add key="some key" xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)"/>
<add key="some key" value="some value" xdt:Transform="Replace" xdt:Locator="Match(key)" />
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-12 09:48:51