首页 新闻 会员 周边

Web.config 的问题。

0
悬赏园豆:30 [已解决问题] 解决于 2015-10-13 14:58

1、Web.config 的根节点必须是 <configuration></configuration> ? 为什么?

2、可以在哪些节点下面添加自定义的节点,我在<system.web>节点下添加自定义节点,说该节点名称未定义,这些是在哪里定义的?

3、config 可以通过在不同文件夹下定义来规定该范围下使用的配置信息,可以举例说明么?

孤独的守候者的主页 孤独的守候者 | 初学一级 | 园豆:48
提问于:2015-09-29 15:25
< >
分享
最佳答案
0

1:第一个问题你没必要问为什么,我们都是用的别人的框架,封装的方法什么的就是这样去读Web.config的节点的,你也可以自己去配置xml,自己去读

2:对于自定义节点一般有2种,也可以根节点不是<configuration></configuration> ,有点像你自己去读

   (一)我觉得简单一点的,继承ConfigurationSection

   (二)其一,继承IConfigurationSectionHandler,实现Create方法,这样就更加灵活了

copy第一种在MSDN上的代码给你看下:

 <configSections>
    <section name="custom" type="SampleWebConfigSection.Configuration.customSection, SampleWebConfigSection" />
  </configSections>
<custom fileName="Default.txt" maxUsers="2500" maxIdleTime="00:10:00" />
using System.Configuration;
using System;

namespace SampleWebConfigSection.Configuration
{
    public class customSection : ConfigurationSection
    {
        [ConfigurationProperty("fileName", DefaultValue = "default.txt", IsRequired = true, IsKey = false)]
        [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
        public string FileName
        {
            get { return (string)this["fileName"]; }
            set { this["fileName"] = value; }
        }

        [ConfigurationProperty("maxUsers", DefaultValue = (long)10000, IsRequired = false)]
        [LongValidator(MinValue = 1, MaxValue = 10000000, ExcludeRange = false)]
        public long MaxUsers
        {
            get { return (long)this["maxUsers"]; }
            set { this["maxUsers"] = value; }
        }

        [ConfigurationProperty("maxIdleTime", DefaultValue = "0:10:0", IsRequired = false)]
        [TimeSpanValidator(MinValueString = "0:0:30", MaxValueString = "5:00:0", ExcludeRange = false)]
        public TimeSpan MaxIdleTime
        {
            get { return (TimeSpan)this["maxIdleTime"]; }
            set { this["maxIdleTime"] = value; }
        }
    }
}
 customSection custom = (customSection)System.Configuration.ConfigurationManager.GetSection("custom");
    Response.Write(custom.FileName + "|" + custom.MaxUsers.ToString() + "|" + custom.MaxIdleTime);

3:转个连接给你http://www.cnblogs.com/freeliver54/archive/2007/03/03/662627.html

收获园豆:25
稳稳的河 | 老鸟四级 |园豆:4216 | 2015-09-29 17:13
其他回答(1)
0

至于你问的为什么,没有办法回到你,微软的东西只能去问微软了。

我2个月前也不知道怎么在配置文件中写配置,现在我知道了,例如我要写一个连接字符串,直接添加一个名为connectionStrings的节点,在此节点中添加<add name="配置名称" connectionString="......." />,获取的时候用ConfigurationManager.ConnectionStrings["配置名称"].ConnectionString;

在appSettings中也是相同的操作,不过获取的时候有所差别,ConfigurationManager.AppSettings["配置名称"];

之所以这样写,是因为他有现成的方法获取到

 

如果不用他的config文件也可以自己写成Xml文件,然后自己写方法获取就是了

收获园豆:5
如此低调的男人 | 园豆:842 (小虾三级) | 2015-09-29 17:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册