返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

ASP.NET MVC自定义配置基础教程

时间:2016-10-07 21:37来源:知行网www.zhixing123.cn 编辑:麦田守望者

web.config是ASP.NET MVC的配置文件,那么程序中用到了配置项,怎么写入这个文件?又怎么读出来呢?本文将介绍配置文件的两种简单应用,更详尽的内容请参照MSDN。

一、      写配置文件

我用MySet1为例子介绍第一种用法,即只需要单条的配置信息;用MySet2介绍第二种用法,即类似的配置项可能有多条。配置文件编写如下:
<configuration>
  <configSections>
        <section name="MySet1" type="MyMVC.Web.Models.MySet1" />
<section name="MySet2" type="MyMVC.Web.Models.MySet2" />
  </configSections>
  < MySet1 Name="aa" Value="22"/>
  < MySet2>
    <set2 Name="aa" MaxValue="10" MinValue="1"/>
    <set2 Name="bb" MaxValue="20" MinValue="12"/>
  </ MySet2>
</configuration>
说明:这里的节点名称都是需要声明类型的,声明写在configSections节点下,节点MySet1和MySet2的类型是我们自定义的类,继承ConfigurationSection。
二、      类型定义
1.       MySet1
   public class MySet1: ConfigurationSection
    {
        [ConfigurationProperty("Name", IsRequired = true)]
        public String Name
        {
            get
            { return (String)this["Name "]; }
            set
            { this["Name "] = value; }
        }
 
        [ConfigurationProperty("Value", IsRequired = true)]
        public String Value
        {
            get
            { return (String)this["Value "]; }
            set
            { this["Value "] = value; }
        }
    }

2.       MySet2
配置文件中,整个< MySet2/>节点读到内存中就是类型MySet2的一个对象;节点下的所有子节点可以看做是MySet2的属性,这个属性的类型我们定义为MySet2Collection(配置集合);每一个子节点的类型定义为MySet2Element(配置项)。

a )   取值范围类RangeLimit,供使用的最终形态
    public class RangeLimit
    { 
        public string Name { get; set; }
        public int MaxValue { get; set; }
        public int MinValue { get; set; }
    }
b)        section类型MySet2
    public class MySet2: ConfigurationSection
    {
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public MySet2Collection MySet2s
        {
            get
            {               
                return (MySet2Collection)base[""];
            }
        }
 
        public List<RangeLimit> GetList()
        {
            List< RangeLimit > settings = new List< RangeLimit >();
            for (int i = 0; i < MySet2s.Count; i++)
            {
                var element = MySet2s[i];
                settings.Add(new RangeLimit
                {
                    Name = MySet2s[i].SubjectName,
                    MaxValue = Int32.Parse(MySet2s[i].MaxValue),
                    MinValue = Int32.Parse(MySet2s[i].MinValue)
                });
            }
            return settings;
        }
}
c)         配置集合类MySet2Collection
    public class MySet2Collection: ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
 
        protected override ConfigurationElement CreateNewElement()
        {
            return new MySet2ElementElement();
        }
 
        public new int Count
        {
            get { return base.Count; }
        }
 
        public MySet2ElementElement this[int index]
        {
            get
            {
                return (MySet2ElementElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
 
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (MySet2ElementElement)element;
        }
 
        protected override string ElementName
        {
            get
            {
                return "set2";
            }
        }
}
d)        配置项类MySet2Element
    public class MySet2Element: ConfigurationElement
    {
        [ConfigurationProperty("Name", IsRequired = true)]
        public String Name
        {
            get
            { return (String)this["Name "]; }
        }
 
        [ConfigurationProperty("MaxValue", IsRequired = true)]
        public String MaxValue
        {
            get
            { return (String)this["MaxValue "]; }
        }
 
        [ConfigurationProperty("MinValue", IsRequired = true)]
        public String MinValue
        {
            get
            { return (String)this["MinValue "]; }
        }
}
备注:定义属性时也可以加上验证标签,详见MSDN。

三、      配置项读取

1.       MySet1
var section = (MyMVC.Web.Models.MySet1)ConfigurationManager.GetSection("MySet1");

2.       MySet2
      var section = (MyMVC.Web.Models.MySet2)ConfigurationManager.GetSection("MySet2");
      List<MyMVC.Web.Models.RangeLimit> settings = section.GetList();
顶一下
(3)
100%
踩一下
(0)
0%
标签(Tag):C#
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片