简体   繁体   中英

updating values in web.config

I need to store an escaped html string in a key in web.config using the KeyValueConfigurationElement.Save method built into framework 3.5. But when I try to do so, it keeps escaping my ampersands.

Code looks like this:

strHTML = DecodeFTBInput(FTB1.Text)

FTB1.Text is a string of HTML, like this: <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 string and function, let's say strHTML now 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 edit web.config to store the correct value, but I need the authenticated admin user to be able to change the html themselves.

The problem is that when I try to save this string into its key in web.config, it escapes all ampersands as &amp; which ruins the string.

How can I get around this?

web.config is an XML file, so when it writes values there the .NET Framework stores strings using HTML encoding , replacing the < > & characters with &lt; , &gt; and &amp; , and much more besides.

You'll need to stop your DecodeFTPInput method from HTML encoding the string if you want the HTML in the web.config file to be editable. Otherwise you'll be HTML encoding twice, which isn't the result you want!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM