views:

105

answers:

3

Hi, i've to display a messagge in my homepage (default.aspx) different for each "installation" of my web app. i would like to avoid to make a call to database to show this message.. so i've thougth to use web.config to store something like this

<add key="WelcomeString" value="lorem ipsus <b>doloret sit amen</b>" />

But i've noticed i can't use html markup into web.config ... Is there a better approach ? Or is there a way to insert html markup into web.config ? Thank you again stack overflow guru's... i'm learning from you a lot of things !

+3  A: 

You need to XML encode it, to store it in the XML as a valid attribute value. eg:

<add key="WelcomeString" value="lorem ipsus &lt;b&gt;doloret sit amen&lt;/b&gt;" />
Rowland Shaw
Does the Server.HTMLEncode method accomplish the same thing as an XMLEncode, or are there differences?
Rice Flour Cookies
@Rising Star They're very similar, but subtly different. Either way, app.config is an XML file, so it needs to be XML encoded (so don't try and use HTML entities like `©` or ` `)
Rowland Shaw
+2  A: 

Use "&lt;" and "&gt;" instead of "<" and ">":

<add key="WelcomeString" value="lorem ipsus &lt;b&gt;doloret sit amen&lt;/b&gt;" />
Fyodor Soikin
wow guys, stackoverflow is "THE" bible for a programmer! ;) thanks again!
stighy
+2  A: 

You have a couple of examples of how to add it to the web.config file, but I would suggest that you consider adding a "localization" XML file to App_Data and read it from there rather than polluting the web.config file with customizations for each installation. You could read this file during application start up and store the values in the HttpRuntime.Cache by key, retrieving them from there as needed. Note that you need a way to regenerate them if they get flushed from the Cache (or mark them as not removable). Use the same technique to encode it for an attribute in the XML file or, if longer, store it in CDATA in the node value.

I use a technique like this with two XML files, defaults and localizations. Defaults supplies default values for the localizable aspects of the application. Localizations, if present, will override the defaults. These are loaded, in my case, into a Singleton object for the application that has strongly-typed properties for the values. Note that this encompasses much more than simply localized strings; they can be arbitrarily complex. The Singleton object has methods to read and apply both defaults and localizations given the path to the XML file.

tvanfosson