Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store a dictionary object in my web.config file?

I'd like to store a simple key/value string dictionary in my web config file. Visual Studio makes it easy to store a string collection(see sample below) but I'm not sure how to do it with a dictionary collection.

        <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">           <string>value1</string>           <string>value2</string>           <string>value2</string>         </ArrayOfString> 
like image 646
TGnat Avatar asked Dec 03 '08 18:12

TGnat


People also ask

Can we store anything other than string or INT in dictionary key?

You can use a Dictionary<string,object> and then you can put anything you want into it.

Which file is used to store settings of the ASP Net website?

web. config file is an XML-based configuration file used in ASP. NET-based applications to manage various settings that are concerned with the configuration of our website.

Where are app config files stored?

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.


2 Answers

Why reinvent the wheel? The AppSettings section is designed for exactly the purpose of storing dictionary-like data in your config file.

If you don't want to put too much data in your AppSettings section, you can group your related values into their own section as follows:

<configuration>   <configSections>     <section        name="MyDictionary"        type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />   </configSections>    <MyDictionary>      <add key="name1" value="value1" />      <add key="name2" value="value2" />      <add key="name3" value="value3" />      <add key="name4" value="value4" />   </MyDictionary> </configuration> 

You can access elements in this collection using

using System.Collections.Specialized; using System.Configuration;  public string GetName1() {     NameValueCollection section =         (NameValueCollection)ConfigurationManager.GetSection("MyDictionary");     return section["name1"]; } 
like image 177
Juliet Avatar answered Sep 19 '22 10:09

Juliet


Juliet's answer is on point, but FYI you can also put additional configs in external .config files, by setting up your web.config as follows:

<?xml version="1.0"?> <configuration>   <configSections>     <!-- blah blah the default stuff here -->      <!-- here, add your custom section -->     <section name="DocTabMap" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />   </configSections>    <!-- your custom section, but referenced in another file -->   <DocTabMap file="CustomDocTabs.config" />    <!-- etc, remainder of default web.config is here --> </configuration> 

Then, your CustomDocTabs.config looks like this:

<?xml version="1.0"?> <DocTabMap>   <add key="A" value="1" />   <add key="B" value="2" />   <add key="C" value="3" />   <add key="D" value="4" /> </DocTabMap> 

Now you can access it in code via:

NameValueCollection DocTabMap = ConfigurationManager.GetSection("DocTabMap") as NameValueCollection; DocTabMap["A"] // == "B" 
like image 37
Factor Mystic Avatar answered Sep 20 '22 10:09

Factor Mystic