如何以编程方式读取asp.net mvc中的自定义配置文件

问题描述 投票:0回答:3

在我的ASP.NET MVC 5的根文件夹中,我有两个配置文件。一个是默认的web.config文件,第二个是department.config

department.config文件的内容是:

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

如何读取department.config文件?

我想在此配置文件中获取<department>下的值集合。

编辑:Web.config有<department configSource="reports.config" />所以,如何在asp.net mvc中读取configSource文件?

编辑:

<configuration>
  <configSections>
    <section name="departments" 
             type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
             restartOnExternalChanges="false" 
             requirePermission="false" />
c# asp.net asp.net-mvc web-config
3个回答
3
投票

为什么不在web.config中使用appSettings部分?使用ConfigurationManager对象可以轻松读取这些内容。

在您的web.config中,找到appSettings部分:

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

然后在您想要读取它的类中,导入正确的命名空间:

using System.Configuration;

然后轻松读取值,如下所示:

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

如果你必须在单独的配置中使用它,你可以考虑为它创建一个类,我将很快发布一个例子。

edit1:这是一个如何进行自定义配置的快速示例

首先,定义配置类:

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;
            }
        }
    }
}

然后,在web.config文件中进行设置:

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

然后你可以很容易地调用它,以及对许多类型的强类型和支​​持:

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

我就是这样做的。使用内置的东西并为它创建一个类。易于配置转换,易于阅读和易于使用。


2
投票

在web.config中,您可以指定内置ConfigurationManager可以轻松访问的其他文件。例如,我们决定将连接字符串和应用程序设置分成单独的文件。您可以通过将其放在web.config中来完成此操作:

 <appSettings configSource="appsettings.config" />

然后,您可以以“常规”方式访问这些值

ConfigurationManager.AppSettings["KeyHere"] 

连接字符串也一样......


0
投票

这是一个古老的问题但是如果有人需要知道......第一个geekzsters答案列出了如何编写配置类。然后指定自定义配置节,并在单独的文件中为其指定configSource。

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

然后在您的自定义配置文件中编写一个普通的xml配置

<?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>
© www.soinside.com 2019 - 2024. All rights reserved.