在web中使用HTML标记。配置文件

时间:2023-01-28 20:20:34

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

我想在我的主页上显示一条消息(default.aspx),对于我的web应用的每个“安装”都是不同的。所以我想到了使用网络。配置来存储这样的东西

<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 !

但是我注意到我不能在web中使用html标记。配置…有没有更好的方法,或者有办法将html标记插入到web.config中?再次感谢堆栈溢出大师的…我从你那里学到了很多东西!

3 个解决方案

#1


15  

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

您需要对其进行XML编码,将其作为有效的属性值存储在XML中。例如:

<add key="WelcomeString" value="lorem ipsus &lt;b&gt;doloret sit amen&lt;/b&gt;" />

#2


11  

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

使用"<"和">"而不是"<"和">":

<add key="WelcomeString" value="lorem ipsus &lt;b&gt;doloret sit amen&lt;/b&gt;" />

#3


4  

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.

您有几个如何将其添加到web的示例。配置文件,但是我建议您考虑向App_Data添加一个“本地化”XML文件,然后从那里读取它,而不是污染web。配置文件,为每个安装定制。您可以在应用程序启动期间读取该文件,并将值存储在HttpRuntime中。按键缓存,根据需要从那里检索它们。注意,如果它们从缓存中刷新(或者标记为不可移动),您需要一种重新生成它们的方法。使用相同的技术对XML文件中的属性进行编码,或者,如果更长的时间,将其存储在节点值的CDATA中。

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.

我对两个XML文件(默认值和本地化)使用了类似的技术。默认值为应用程序的可本地化方面提供默认值。本地化,如果存在,将覆盖默认值。在我的例子中,它们被加载到应用程序的单例对象中,该对象具有值的强类型属性。注意,这不仅仅包含本地化的字符串;它们可以是任意复杂的。在给定XML文件路径的情况下,单例对象具有读取和应用默认值和本地化的方法。

#1


15  

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

您需要对其进行XML编码,将其作为有效的属性值存储在XML中。例如:

<add key="WelcomeString" value="lorem ipsus &lt;b&gt;doloret sit amen&lt;/b&gt;" />

#2


11  

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

使用"<"和">"而不是"<"和">":

<add key="WelcomeString" value="lorem ipsus &lt;b&gt;doloret sit amen&lt;/b&gt;" />

#3


4  

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.

您有几个如何将其添加到web的示例。配置文件,但是我建议您考虑向App_Data添加一个“本地化”XML文件,然后从那里读取它,而不是污染web。配置文件,为每个安装定制。您可以在应用程序启动期间读取该文件,并将值存储在HttpRuntime中。按键缓存,根据需要从那里检索它们。注意,如果它们从缓存中刷新(或者标记为不可移动),您需要一种重新生成它们的方法。使用相同的技术对XML文件中的属性进行编码,或者,如果更长的时间,将其存储在节点值的CDATA中。

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.

我对两个XML文件(默认值和本地化)使用了类似的技术。默认值为应用程序的可本地化方面提供默认值。本地化,如果存在,将覆盖默认值。在我的例子中,它们被加载到应用程序的单例对象中,该对象具有值的强类型属性。注意,这不仅仅包含本地化的字符串;它们可以是任意复杂的。在给定XML文件路径的情况下,单例对象具有读取和应用默认值和本地化的方法。