Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use HTML markup in web.config file

I want to display a message in my homepage (default.aspx), which is different for each "installation" of my web app. I would like to avoid making a call to the database to show this message.. so I thought of using 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 in the 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 !

like image 659
stighy Avatar asked May 28 '10 20:05

stighy


3 Answers

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;" />
like image 56
Rowland Shaw Avatar answered Oct 20 '22 02:10

Rowland Shaw


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

<add key="WelcomeString" value="lorem ipsus &lt;b&gt;doloret sit amen&lt;/b&gt;" />
like image 39
Fyodor Soikin Avatar answered Oct 20 '22 01:10

Fyodor Soikin


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.

like image 5
tvanfosson Avatar answered Oct 20 '22 03:10

tvanfosson