ASP.NET-talk - talking about everything from web.config (2) (ConfigSections Detailed - in)

We then on a continued, on the one described in ConfigSection structure and two simple DEMO, Benpian said about SectionGroup, ConfigurationElementCollection and key / value pair configurationsection.

usage of.

1, SectionGroup use


 

The following code simply explain the use SectionGroup:

1), first define a class that inherits ConfigurationSectionGroup:

   1:   /// <summary>
   2:    /// Custom SectionGroup
   3:   /// </summary>
   4:      public class MySectionGroup:ConfigurationSectionGroup
   5:      {
   6:          [ConfigurationProperty("myBlog")]
   7:          public MySection MyBlog
   8:          {
   9:              get
  10:              {
  11:                  return (MySection)base.Sections["myBlog"];
  12:              }
  13:          }
  14:      }

2), followed by a succession of Section ConfigurationSection in the definition:

   1:  /// <summary>
   2:       /// Custom Section
   3:      /// </summary>
   4:      public class MySection:ConfigurationSection
   5:      {
   6:          [ConfigurationProperty("name")]
   7:           public string Name Blog
   8:          {
   9:              get
  10:              {
  11:                  return (string)base["name"];
  12:              }
  13:          }
  14:          [ConfigurationProperty("url")]
  15:          public string BlogUrl
  16:          {
  17:              get
  18:              {
  19:                  return (string)base["url"];
  20:              }
  21:          }
  22:      }

The following talk more about how to use and how to configure in web.config:

web.config configuration:

   1:  <configSections>
   2:          <sectionGroup name="myBlogs" type="KevinDiao.MySectionDemo01.MySectionGroup,KevinDiao.MySectionDemo01">
   3:              <section name="myBlog" type="KevinDiao.MySectionDemo01.MySection,KevinDiao.MySectionDemo01"/>
   4:          </sectionGroup>
   5:  </configSections>
   6:   
   7:  <myBlogs>
   8:      <myBlog name="五香瓜子" url="http://www.cnblogs.com/diaojia/"></myBlog>
   9:  </myBlogs>

Read web.config configuration:

   1:   MySection mySection = ConfigurationManager.GetSection("myBlogs/myBlog") as MySection;
   2: Response.Write ( "blog name:" mySection.BlogName + + "a");
   3:  Response.Write("博客地址:<a href='" + mySection.BlogUrl + "'>" + mySection.BlogUrl + "</a>");

 

Run the results obtained:

Blog name: Spiced seeds
Blog address: HTTP: //www.cnblogs.com/diaojia/
 

2, ConfigurationElementCollection use


 

    Here again discuss how to configure custom collection in ConfigSections, we still use the code illustrate.

   1), first define a class inherit ConfigurationElement.

   1:      /// <summary>
   2:       /// Custom Element
   3:      /// </summary>
   4:      public class MyBlogElement:ConfigurationElement
   5:      {
   6:          [ConfigurationProperty("name")]
   7:          public string Name
   8:          {
   9:              get
  10:              {
  11:                  return (string)base["name"];
  12:              }
  13:          }
  14:          [ConfigurationProperty("url")]
  15:          public string Url
  16:          {
  17:              get
  18:              {
  19:                  return (string)base["url"];
  20:              }
  21:          }
  22:      }

It consists primarily of the main content you want to configure.

2), followed by the definition of a class that inherits ConfigurationElementCollection

   1:      /// <summary>
   2:       /// Custom ElementCollection
   3:      /// </summary>
   4:      public class MyBlogElementCollection:ConfigurationElementCollection
   5:      {
   6:          protected override ConfigurationElement CreateNewElement()
   7:          {
   8:              return new MyBlogElement();
   9:          }
  10:          protected override object GetElementKey(ConfigurationElement element)
  11:          {
  12:              return ((MyBlogElement)element).Name;
  13:          }
  14:          public override ConfigurationElementCollectionType CollectionType
  15:          {
  16:              get
  17:              {
  18:                  return ConfigurationElementCollectionType.BasicMap;
  19:              }
  20:          }
  21:   
  22:          protected override string ElementName
  23:          {
  24:              get
  25:              {
  26:                  return "myBlog";
  27:              }
  28:          }
  29:          public MyBlogElement this[int index]
  30:          {
  31:              get
  32:              {
  33:                  return (MyBlogElement)BaseGet(index);
  34:              }
  35:              set
  36:              {
  37:                  if (BaseGet(index) != null)
  38:                  {
  39: BaseRemoveAt (index);
  40:                  }
  41:                  BaseAdd(index, this);
  42:              }
  43:          }
  44:      }

3) once again define a class that inherits ConfigurationSection

   1:      /// <summary>
   2:       /// Custom Section
   3:      /// </summary>
   4:      public class MyBlogSection:ConfigurationSection
   5:      {
   6:          [ConfigurationProperty("name")]
   7:          public string Name
   8:          {
   9:              get
  10:              {
  11:                  return (string)base["name"];
  12:              }
  13:          }
  14:          [ConfigurationProperty("",IsDefaultCollection=true)]
  15:          public MyBlogElementCollection MyBlogCollection
  16:          {
  17:              get
  18:              {
  19:                  return (MyBlogElementCollection)base[""];
  20:              }
  21:          }
  22:   
  23:      }

Here a look at how how to configure in web.config:

   1:  <configSections>
   2:  <section name ="MyBlogs"  type="KevinDiao.MySectionDemo02.MyBlogSection,KevinDiao.MySectionDemo02"/>
   3:  </configSections>
   4:   <MyBlogs name="KevinDiao">
   5:      <myBlog name="五香瓜子" url="http://www.cnblogs.com/diaojia/"></myBlog>
   6:      <myBlog name="业精于勤" url="http://diaojia.blog.51cto.com/"></myBlog>
   7:    </MyBlogs>

Look at how read:

   1:   MyBlogSection mySection = ConfigurationManager.GetSection("MyBlogs") as MyBlogSection;
   2:   foreach (MyBlogElement item in mySection.MyBlogCollection)
   3:    {
   . 4: Response.Write ( "blog name:" item.name + + "a");
   5:         Response.Write("博客地址:<a href='" + item.Url + "'>" + item.Url + "</a>" + "<br/>");
   6:         Response.Write("-----------------------------------------------------------------<br/>");
   7:     }

Finally, look at the results of the operation:

Blog name: Spiced seeds
Blog address: HTTP: //www.cnblogs.com/diaojia/
-----------------------------------------------------------------
Blog name: diligence
Blog address: HTTP: //diaojia.blog.51cto.com/
-----------------------------------------------------------------

3, key / value pair configurationsection used (e.g.: appSettings)


 

Finally, explain how to use a simple key / value pair configurationsection in web.config.

1), first define a class inherit ConfigurationElement

   1:      /// <summary>
   2:       /// custom ConfigurationElement
   3:      /// </summary>
   4:      public class MyBlogElement: ConfigurationElement
   5:      {
   6:          [ConfigurationProperty("name")]
   7:          public string Name
   8:          {
   9:              get
  10:              {
  11:                  return (string)base["name"];
  12:              }
  13:          }
  14:          [ConfigurationProperty("url")]
  15:          public string Url
  16:          {
  17:              get
  18:              {
  19:                  return (string)base["url"];
  20:              }
  21:          }       
  22:      }

In fact, and in 2, ConfigurationElementCollection of the same.

2), followed by a succession of ConfigurationElementCollection class definition:

   1:      /// <summary>
   2:       /// Custom ConfigurationElementCollection
   3:      /// </summary>
   4:      public class MyBlogsElectionCollection:ConfigurationElementCollection
   5:      {
   6:          protected override ConfigurationElement CreateNewElement()
   7:          {
   8:              return new MyBlogElement();
   9:          }
  10:   
  11:          protected override object GetElementKey(ConfigurationElement element)
  12:          {
  13:              return ((MyBlogElement)element).Name;
  14:          }
  15:      }

 

3), again it is the definition of a class that inherits ConfigurationSection

   1:      /// <summary>
   2:       /// Custom Section
   3:      /// </summary>
   4:      public class MyBlogsSection : ConfigurationSection
   5:      {
   6:          [ConfigurationProperty("name")]
   7:          public string Name
   8:          {
   9:              get
  10:              {
  11:                  return (string)base["name"];
  12:              }
  13:          }
  14:          [ConfigurationProperty("",IsDefaultCollection=true)]
  15:          public MyBlogsElectionCollection MyBlogs
  16:          {
  17:              get 
  18:              {
  19:                  return (MyBlogsElectionCollection)base[""];
  20:              }
  21:          }
  22:      }

Here a look at how how to configure in web.config:

   1:  <configSections>
   2:   <section name="MyBlogs" type="KevinDiao.MySectionDemo03.MyBlogsSection,KevinDiao.MySectionDemo03"/>
   3:  </configSections>
   4:   <MyBlogs name="KevinDiao">
   5:      <add name="五香瓜子" url="http://www.cnblogs.com/diaojia/"></add>
   6:      <add name="业精于勤" url="http://diaojia.blog.51cto.com/"></add>
   7:   </MyBlogs>

Look at how read:

   1:   MyBlogsSection mySection = ConfigurationManager.GetSection("MyBlogs") as MyBlogsSection;
   2:    foreach (MyBlogElement item in mySection.MyBlogs)
   3:    {
   . 4: Response.Write ( "blog name:" item.name + + "a");
   5:        Response.Write("博客地址:<a href='" + item.Url + "'>" + item.Url + "</a>" + "<br/>");
   6:        Response.Write("-----------------------------------------------------------------<br/>");
   7:    }

Finally, look at the results of the operation:

Blog name: Spiced seeds
Blog address: HTTP: //www.cnblogs.com/diaojia/
-----------------------------------------------------------------
Blog name: diligence
Blog address: HTTP: //diaojia.blog.51cto.com/
-----------------------------------------------------------------
 
 

Finally, attach Benpian Code: DEMO

REFERENCE FROM : http://www.cnblogs.com/diaojia/archive/2011/04/06/2007350.html

reference:

MSDN

Reproduced in: https: //www.cnblogs.com/zhangchenliang/p/4155347.html

Guess you like

Origin blog.csdn.net/weixin_33995481/article/details/93495474