2012-06-14 33 views
193

Có thể chuyển đổi các tập tin Web.config appSettings sau:Làm thế nào để thay đổi giá trị của thuộc tính trong phần appSettings với biến đổi Web.config

<appSettings> 
    <add key="developmentModeUserId" value="00297022" /> 
    <add key="developmentMode" value="true" /> 
    /* other settings here that should stay */ 
</appSettings> 

vào một cái gì đó như thế này:

<appSettings> 
    <add key="developmentMode" value="false" /> 
    /* other settings here that should stay */ 
</appSettings> 

Vì vậy, tôi cần phải xóa khóa developmentModeUserId và tôi cần phải thay thế giá trị cho khóa developmentMode.

Trả lời

345

Bạn muốn một cái gì đó như:

<appSettings> 
    <add key="developmentModeUserId" xdt:Transform="Remove" xdt:Locator="Match(key)"/> 
    <add key="developmentMode" value="false" xdt:Transform="SetAttributes" 
      xdt:Locator="Match(key)"/> 
</appSettings> 

Xem http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx để biết thêm.

+20

Lưu ý các phím có phân biệt chữ hoa chữ thường! – Cosmin

+1

Câu trả lời hay. Tôi đã thử các tùy chọn của bên thứ 3 như Cheetah Chậm và không nơi nào - điều này thật đơn giản và hoàn hảo. – Steve

+2

@stevens: Bạn cần Cheetah Chậm nếu bạn muốn chuyển đổi, ví dụ như app.config cho các ứng dụng gốc. Cú pháp, tuy nhiên, nên giống hệt nhau nếu tôi nhớ lại (nó đã được một thời gian kể từ khi tôi đã phải sử dụng Cheetah chậm). – Ellesedil

0

Thay thế tất cả AppSettings

This is the overkill case where you just want to replace an entire section of the web.config. In this case I will replace all AppSettings in the web.config will new settings in web.release.config. This is my baseline web.config appSettings: 


<appSettings> 
    <add key="KeyA" value="ValA"/> 
    <add key="KeyB" value="ValB"/> 
</appSettings> 

Now in my web.release.config file, I am going to create a appSettings section except I will include the attribute xdt:Transform=”Replace” since I want to just replace the entire element. I did not have to use xdt:Locator because there is nothing to locate – I just want to wipe the slate clean and replace everything. 


<appSettings xdt:Transform="Replace"> 
    <add key="ProdKeyA" value="ProdValA"/> 
    <add key="ProdKeyB" value="ProdValB"/> 
    <add key="ProdKeyC" value="ProdValC"/> 
</appSettings> 



Note that in the web.release.config file my appSettings section has three keys instead of two, and the keys aren’t even the same. Now let’s look at the generated web.config file what happens when we publish: 


<appSettings> 
    <add key="ProdKeyA" value="ProdValA"/> 
    <add key="ProdKeyB" value="ProdValB"/> 
    <add key="ProdKeyC" value="ProdValC"/> 
</appSettings> 

Cũng giống như chúng ta mong đợi - appSettings web.config đã hoàn toàn thay thế bằng các giá trị trong web.release config. Điều đó thật dễ dàng!

Các vấn đề liên quan