Updating values โ€‹โ€‹in web.config

I need to save the escaped html string in a key in web.config using the KeyValueConfigurationElement.Save Method built into the 3.5 framework. But when I try to do this, he continues to run away from my ampersands.

The code is as follows:

strHTML = DecodeFTBInput(FTB1.Text)

FTB1.Text is an HTML string, for example: <b><font color="#000000">Testing</font></b>

DecodeFTPInput uses the String.Replace () method to change <and> to &lt;and &gt;, and "to &quot;.

Given the above line and function, let's say it strHTMLnow contains the following:

&lt;b&gt;&lt;font color=&quot;#000000&quot;&gt;Testing&lt;/font&gt;&lt;/b&gt;

Of course, I can manually change web.config to save the correct value, but I need an authenticated admin user to be able to change the html itself.

The problem is that when I try to save this line in its key in web.config, it avoids all ampersands like &amp;that which breaks the line.

How can I get around this?

+3
source share
1 answer

web.config- is an XML file, so when he writes the values, .NET Framework stores strings using HTML-coding , replacing the < > &characters with &lt;, &gt;and &amp;and more.

You need to stop the DecodeFTPInput method from HTML encoding the string if you want the HTML in the web.config file to be edited. Otherwise, you will encode HTML twice, which is not the desired result!

+1
source

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


All Articles