MS_Tech_Programmer
MS_Tech_Programmer

Reputation: 53

How to programmatically read the custom config files in asp.net mvc

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" />

Upvotes: 0

Views: 13170

Answers (4)

Deepak Verma
Deepak Verma

Reputation: 617

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.

Upvotes: 0

reachingnexus
reachingnexus

Reputation: 101

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>

Upvotes: 1

geekzster
geekzster

Reputation: 559

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.

Upvotes: 3

Bob Mac
Bob Mac

Reputation: 359

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...

Upvotes: 3

Related Questions