Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web.config appSettings: complex values

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

like image 340
pistacchio Avatar asked Jun 09 '11 06:06

pistacchio


1 Answers

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; }
}
like image 122
Alex Aza Avatar answered Sep 22 '22 19:09

Alex Aza