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

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
pistacchio
  • 56,889
  • 107
  • 278
  • 420

3 Answers3

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; }
}
Community
  • 1
  • 1
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • is this web.config specific? i dont think so – naveen Jun 09 '11 at 06:44
  • @naveen - what do you mean? It will work for any application, web, windows, console. – Alex Aza Jun 09 '11 at 06:47
  • the question is about appsettings in the web.config. – naveen Jun 09 '11 at 06:48
  • @naveen - `appSettings` is the 'old' and limited approach for storing settings. `applicationSettings` is the newer, more flexible, supports designer, etc, etc. – Alex Aza Jun 09 '11 at 06:50
  • id indeed this can be done in asp.net, could you possibly show an example? – naveen Jun 09 '11 at 06:58
  • @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. – Alex Aza Jun 09 '11 at 07:00
  • 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? – naveen Jun 09 '11 at 07:08
  • @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? – Alex Aza Jun 09 '11 at 07:12
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.

naveen
  • 53,448
  • 46
  • 161
  • 251
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.

ChristiaanV
  • 5,401
  • 3
  • 32
  • 42