当前位置:编程学习 > asp >>

用自定义的节来扩展web.config和machine.config配置文件的结构

webconfig:

[html] 
<?xml version="1.0"?> 
<!-- 
  有关如何配置 ASP.NET 应用程序的详细信息,
  --> 
<configuration> 
    <configSections> 
        <section name="RemotingObject" type="RemotingObject"/> 
    </configSections> 
    <system.web> 
        <compilation debug="true" targetFramework="4.0"/> 
    </system.web> 
    <RemotingObject available="true" pollTimeout="00:01:00" location="tcp://OrderComputer:8010/OrderService"/> 
</configuration> 

 获取自定义节点 内容:

[csharp]
//打开配置文件 
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/"); 
//获取配置节对象 
RemotingObject custSection = 
(RemotingObject)config.GetSection("RemotingObject"); 
//显示配置节信息 
lblInfo.Text += "获取自定义配置节的信息...<br />" + 
"<b>位置:</b> " + custSection.Location + 
"<br /><b>是否可用:</b> " + custSection.Available.ToString() + 
"<br /><b>超时时间:</b> " + custSection.PollTimeout.ToString() + "<br /><br />"; 

RemotingObject对象类

[csharp] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Configuration; 
 
/// <summary> 
///RemotingObject 的摘要说明 
/// </summary> 
public class RemotingObject : ConfigurationSection 

    //定义远程对象是否可用 
    [ConfigurationProperty("available", IsRequired = false, DefaultValue = true)] 
    public bool Available 
    { 
        get { return (bool)base["available"]; } 
        set { base["available"] = value; } 
    } 
    //定义远程对象的超时时间 
    [ConfigurationProperty("pollTimeout", IsRequired = true)] 
    public TimeSpan PollTimeout 
    { 
        get { return (TimeSpan)base["pollTimeout"]; } 
        set { base["pollTimeout"] = value; } 
    } 
    //定义远程对象所在的位置 
    [ConfigurationProperty("location", IsRequired = true)] 
    public string Location 
    { 
        get { return (string)base["location"]; } 
        set { base["location"] = value; } 
    } 

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,