0

In the root folder of my ASP.NET MVC 5 I have two config files. one is the default web.config file and the second one is department.config.

The content of the department.config file is:

<department>    
    <add key="dept1" value="xyz.uvw.rst" />   
    <add key="dept2" value="abc.def.ghi" />
<department>

How to read the department.config file ?

I want to get a collection of values under <department> in this config file.

Edit: Web.config has <department configSource="reports.config" /> so, how to read the configSource file in asp.net mvc ?

Edit:

<configuration>
  <configSections>
    <section name="departments" 
             type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
             restartOnExternalChanges="false" 
             requirePermission="false" />
4
  • its pretty much same as reading any XML file in .NET. Commented Jul 17, 2014 at 20:59
  • is there any API for reading such custom config files like we do have for web.config files. if there a way we can tell the WebConfigurationmanager that instead of reading it from web.config read it from department.config Commented Jul 17, 2014 at 21:02
  • you mean something like this: joelabrahamsson.com/… Commented Jul 17, 2014 at 21:06
  • If you want to have separate files for some custom settings and want to manage them separately then use <configSections> Commented Jul 17, 2014 at 21:07

4 Answers 4

3

In your web.config, you can specify other files that the built-in ConfigurationManager can easily access. For example, we decided that we wanted to separate connection strings and application setting into separate files. You can do this by putting this in your web.config:

 <appSettings configSource="appsettings.config" />

Then you can access these values in the 'regular' way

ConfigurationManager.AppSettings["KeyHere"] 

Same thing with connection strings...

Sign up to request clarification or add additional context in comments.

Comments

3

Why not use the appSettings section in your web.config? These are easy to read using ConfigurationManager object.

In your web.config, find the appSettings section:

<appSettings>
<add key="dept1" value="xyz.uvw.rst"/>

Then in your class where you want to read it, import the correct namespace:

using System.Configuration;

And then read the value easily, like so:

var dept1 = ConfigurationManager.AppSettings.Get("dept1");

If you have to have it in a separate config, you might consider creating a class for it, I'll post up an example of that shortly.

edit1: here is a quick example of how to do your own custom config

first, define the config class:

using System;
using System.Configuration;

namespace WebApplication2
{
    public class MyConfig : ConfigurationSection
    {
        private static readonly MyConfig ConfigSection = ConfigurationManager.GetSection("MyConfig") as MyConfig;

        public static MyConfig Settings
        {
            get
            {
                return ConfigSection;
            }
        }


        [ConfigurationProperty("Dept1", IsRequired = true)]
        public string Dept1
        {
            get
            {
                return (string)this["Dept1"];
            }

            set
            {
                this["Dept1"] = value;
            }
        }

        [ConfigurationProperty("Dept2", IsRequired = true, DefaultValue = "abc.def.ghi")]
        public string Dept2
        {
            get
            {
                return (string)this["Dept2"];
            }

            set
            {
                this["Dept2"] = value;
            }
        }
        // added as example of different types
        [ConfigurationProperty("CheckDate", IsRequired = false, DefaultValue = "7/3/2014 1:00:00 PM")]
        public DateTime CheckDate
        {
            get
            {
                return (DateTime)this["CheckDate"];
            }

            set
            {
                this["CheckDate"] = value;
            }
        }
    }
}

Then, set it up in your web.config file:

<configuration>
  <configSections>
    <section name="MyConfig" type="WebApplication2.MyConfig, WebApplication2" />
  </configSections>
  <MyConfig Dept1="xyz.uvw.rst" Dept2="abc.def.ghi" />
...
</configuration>

And then you can call it very easily, along with strong-typing and support for many types:

var dept1 = MyConfig.Settings.Dept1;
var dept2 = MyConfig.Settings.Dept2;
// strongly-typed
DateTime chkDate = MyConfig.Settings.CheckDate;  

That's how I would do it. Use the built-in stuff and create a class for it. Easy to do config transforms with, easy to read, and easy to use.

3 Comments

I cant change these configurations settings and the way it is split into multiple files. thank you for writing that class for me ... I am waiting.
Thank you so much for writing this class for me. but I cant edit the department section or the config class. and I dont want create a property for each entry in department section because this would change frequently and I only need a collection of values in department irrespective of the key. Thank you so much in advance.
The below code worked for me - var departmentSection = ConfigurationManager.GetSection("department") as NameValueCollection; if (departmentSection != null) { foreach (var key in departmentSection.AllKeys) { string department= departmentSection.GetValues(key).FirstOrDefault(); DoSomethingWithDepartments(department); } }
1

This is an old question but in the event someone needs to know... First geekzsters answer lays out how to write a config class. Then you specify a custom Config Section and give it a configSource in a separate file.

<configuration>
  <configSections>
    <section name="myCustomSection" type="MyNamespace.MyCustomType" requirePermission="false" />
  </configSections>
</configuration>
<myCustomSection configSource="myConfigDir\myFile.config" />

Then in your custom config file you write a normal xml config

<?xml version="1.0" encoding="utf-8"?>
<myCustomSection>
  <myCustomType>
   <add name="pro1" value="abc" />
   <add name="prop2" value="cde" />
   <add name="prop3" value="efg" />
  </myCustomType>
</myCustomSection>

1 Comment

For more information on custom config sections for your custom module or class read this: code.tutsplus.com/tutorials/… What I like about this answer here, is it explains how to keep the config section in a separate file, and not to pollute your web.config with massive custom config sections.
0

The simplest way to read configuration File other then Web config is to define file you want to read as follows:

  • webConfig

    <appSettings file="TestFile.config">
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>
    
  • TestFile.Config

     <appSettings>
     <add key="test" value="testData"/>
     </appSettings>
    

Above we define the file we want to read configuration from in Webconfig. thus using ConfigurationManager.AppSettings['test'] you can read the value from your newly created custom config file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.