none
Keys in web.config file RRS feed

  • Question

  • Hi All,

    Is it allowed to have more than one same keys, but have differenct values, below is an example:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
    <add key="Key1" value="value_1" />
    <add key="Key1" value="value_2" />
    </appSettings>
    </configuration>

    I just found this was the scenario in our dev environment, and now quite sure what the purpose was.

    Can someone in here tell me is that legitmate in web.config file?

    Tuesday, November 20, 2012 7:03 AM

Answers

  • NO, you can't have keys with same name. I'm not sure what your scenario is, but if I should guess, you probably shall distinguish between different environements

    <appSettings> <add key="Test.Key1" value="value_1" /> <add key="Prod.Key1" value="value_2" /> </appSettings>

    In this scenario, I have two keys, one for my test environment (Test.Key1) and one key for my production environment (Prod.Key1).

    AIt is also possible to use pre-build events:

    http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx


    Roar Jørstad aka sveroa
    Senior Consultant, EVRY as
    Blog: Notebook, trick & tips
    Please mark as answer or helpful if my post is useful


    • Edited by Roar Jørstad Tuesday, November 20, 2012 7:37 AM
    • Proposed as answer by MasaSam Tuesday, November 20, 2012 10:51 AM
    • Unproposed as answer by MasaSam Tuesday, November 20, 2012 10:55 AM
    • Proposed as answer by MasaSam Tuesday, November 20, 2012 10:55 AM
    • Marked as answer by AndieDu Tuesday, November 20, 2012 11:01 PM
    Tuesday, November 20, 2012 7:14 AM
  • Actually there can be same key twice, it's just that the value of the latest is stored to the AppSettings name/value collection. So in your scenario when key "Key1" is accessed "value_2" is returned. If there would be third "Key1" with value "value_3", then "value_3" would be returned.

    But sveroa is right there usually is no reason to use same key twice, but exception will not be thrown if same key is defined twice.

    • Marked as answer by AndieDu Tuesday, November 20, 2012 11:01 PM
    Tuesday, November 20, 2012 10:59 AM

All replies

  • NO, you can't have keys with same name. I'm not sure what your scenario is, but if I should guess, you probably shall distinguish between different environements

    <appSettings> <add key="Test.Key1" value="value_1" /> <add key="Prod.Key1" value="value_2" /> </appSettings>

    In this scenario, I have two keys, one for my test environment (Test.Key1) and one key for my production environment (Prod.Key1).

    AIt is also possible to use pre-build events:

    http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx


    Roar Jørstad aka sveroa
    Senior Consultant, EVRY as
    Blog: Notebook, trick & tips
    Please mark as answer or helpful if my post is useful


    • Edited by Roar Jørstad Tuesday, November 20, 2012 7:37 AM
    • Proposed as answer by MasaSam Tuesday, November 20, 2012 10:51 AM
    • Unproposed as answer by MasaSam Tuesday, November 20, 2012 10:55 AM
    • Proposed as answer by MasaSam Tuesday, November 20, 2012 10:55 AM
    • Marked as answer by AndieDu Tuesday, November 20, 2012 11:01 PM
    Tuesday, November 20, 2012 7:14 AM
  • Actually there can be same key twice, it's just that the value of the latest is stored to the AppSettings name/value collection. So in your scenario when key "Key1" is accessed "value_2" is returned. If there would be third "Key1" with value "value_3", then "value_3" would be returned.

    But sveroa is right there usually is no reason to use same key twice, but exception will not be thrown if same key is defined twice.

    • Marked as answer by AndieDu Tuesday, November 20, 2012 11:01 PM
    Tuesday, November 20, 2012 10:59 AM
  • thanks guys, i just found that scenario when i use Dictionary data structure to store key/value from web.config file.

    as you know Dictionary data structure throws exception coz it doesnt allow to have identical keys in the collection.

    Anyway, i will get engaged with other developers to find out why there are idential keys in the web.config file, and most likely it was a fault and need to be investigated.


    • Edited by AndieDu Tuesday, November 20, 2012 11:05 PM
    Tuesday, November 20, 2012 11:04 PM