Web.config appSettings: complex values
Asked Answered
L

3

5

Is Web.config's appSettings section only capable of storing simple strings like this?

   <appSettings>
     <add key="OKPage" value="http://mysite.com/okpage.html" />
   </appSettings>

or I can have more complex values like CDATA or nested values? If not, is this the only place in Web.config where to store custom settings? Thanks

Lead answered 9/6, 2011 at 6:33 Comment(0)
R
5

You can make any XmlSerializable class as a setting.

I answered to the similar question here: Custom type application settings in ASP.NET
Also there is a sample project attached.

Here is an example of the settings from my config file:

<setting name="MyEndPoints"
          serializeAs="Xml">
  <value>
    <ArrayOfEndPoint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <EndPoint>
        <HostName>10.40.10.9</HostName>
        <Port>22634</Port>
      </EndPoint>
      <EndPoint>
        <HostName>10.40.10.9</HostName>
        <Port>22635</Port>
      </EndPoint>
    </ArrayOfEndPoint>
  </value>
</setting>

Custom class for settings:

public class EndPoint
{
    public string HostName { get; set; }

    public int Port { get; set; }
}
Respectful answered 9/6, 2011 at 6:39 Comment(8)
is this web.config specific? i dont think soKerek
@naveen - what do you mean? It will work for any application, web, windows, console.Respectful
the question is about appsettings in the web.config.Kerek
@naveen - appSettings is the 'old' and limited approach for storing settings. applicationSettings is the newer, more flexible, supports designer, etc, etc.Respectful
id indeed this can be done in asp.net, could you possibly show an example?Kerek
@naveen - like I mentioned before, it does not matter what kind of project it is. Settings framework works on web applications. I am using it.Respectful
the MSDN link you posted begins like this "The Application Settings feature of Windows Forms...". are you sure you are right? i am intrigued. googled this everywhere and came empty. what does your upvoter have to say?Kerek
@naveen - Post the question, and I will make a step-by-step guide for you, if you promise to mark my answer as an answer. Will this work for you?Respectful
K
5

Keys inside appSettings are retrieved as NameValueCollection which by definition

Represents a collection of associated String keys and String values that can be accessed either with the key or with the index.

So you can have only the data type string as value for an AppSettings key

And yes, AppSettings is the only place where you can store your settings.
MSDN defines AppSettings like this.

Contains custom application settings, such as file paths, XML Web service URLs, or any information that is stored in the.ini file for an application.

Kerek answered 9/6, 2011 at 6:42 Comment(0)
N
0

The AppSettings section is a NameValueCollection which contain strings. (NameValueCollection has an Add(string, string) method.) If you use CDATA inside the key/value than it will be just entered to the collection as a string. You will have to parse it yourself to for example XML.

The AppSetttings section has as a pre for settings that there is already written a wrapper where you can access the keys typesafe from your code. On the other hand your web.config is just XML, where you can add your own types. You will need to write some code to access these sections.

Nosography answered 9/6, 2011 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.