Custom configuration sections allow for a more rigorous specification of configuration settings and can include default and required values. IntelliSense support is also enabled. Here's an example of my blog configuration. See http://mike-ward.net/blog/post/00830/creating-custom-configuration-sections-in-web-config
Last active
December 16, 2015 15:09
-
-
Save mike-ward/5453384 to your computer and use it in GitHub Desktop.
Custom Configuration Section for Web.config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
namespace bloget2.Models | |
{ | |
public class BlogConfiguration : ConfigurationSection | |
{ | |
[ConfigurationProperty("title", IsRequired = true)] | |
public string Title | |
{ | |
get { return this["title"] as string; } | |
} | |
[ConfigurationProperty("description", IsRequired = true)] | |
public string Description | |
{ | |
get { return this["description"] as string; } | |
} | |
[ConfigurationProperty("link", IsRequired = true)] | |
public string Link | |
{ | |
get { return this["link"] as string; } | |
} | |
[ConfigurationProperty("images", IsRequired = true)] | |
public string Images | |
{ | |
// http path to blog images | |
get { return this["images"] as string; } | |
} | |
[ConfigurationProperty("email", IsRequired = true)] | |
public string EMail | |
{ | |
get { return this["email"] as string; } | |
} | |
[ConfigurationProperty("postsPerPage", IsRequired = false, DefaultValue = 3)] | |
public int PostsPerPage | |
{ | |
get { return (int)this["postsPerPage"]; } | |
} | |
[ConfigurationProperty("firstname", IsRequired = true)] | |
public string FirstName | |
{ | |
get { return this["firstname"] as string; } | |
} | |
[ConfigurationProperty("lastname", IsRequired = true)] | |
public string LastName | |
{ | |
get { return this["lastname"] as string; } | |
} | |
[ConfigurationProperty("copyright", IsRequired = true)] | |
public string Copyright | |
{ | |
get { return this["copyright"] as string; } | |
} | |
[ConfigurationProperty("username", IsRequired = true)] | |
public string UserName | |
{ | |
get { return this["username"] as string; } | |
} | |
[ConfigurationProperty("password", IsRequired = true)] | |
public string Password | |
{ | |
get { return this["password"] as string; } | |
} | |
[ConfigurationProperty("categories", IsRequired = false)] | |
public BlogCategoryConfigurationCollection Categories | |
{ | |
get { return this["categories"] as BlogCategoryConfigurationCollection; } | |
} | |
[ConfigurationProperty("timezone", IsRequired = false, DefaultValue = "Eastern Standard Time")] | |
public string TimeZone | |
{ | |
get { return this["timezone"] as string; } | |
} | |
[ConfigurationProperty("dateformat", IsRequired = false, DefaultValue = "MM-dd-yyyy")] | |
public string DateFormat | |
{ | |
get { return this["dateformat"] as string; } | |
} | |
[ConfigurationProperty("adsPerPage", IsRequired = false, DefaultValue = 2)] | |
public int AddsPerPage | |
{ | |
get { return (int)this["adsPerPage"]; } | |
} | |
} | |
public class BlogCategoryConfigurationElement : ConfigurationElement | |
{ | |
[ConfigurationProperty("id", IsRequired = true)] | |
public string Id | |
{ | |
get { return this["id"] as string; } | |
} | |
[ConfigurationProperty("title", IsRequired = true)] | |
public string Title | |
{ | |
get { return this["title"] as string; } | |
} | |
[ConfigurationProperty("description", IsRequired = true)] | |
public string Description | |
{ | |
get { return this["description"] as string; } | |
} | |
} | |
public class BlogCategoryConfigurationCollection : ConfigurationElementCollection | |
{ | |
public BlogCategoryConfigurationElement this[int index] | |
{ | |
get { return BaseGet(index) as BlogCategoryConfigurationElement; } | |
set | |
{ | |
if (BaseGet(index) != null) | |
{ | |
BaseRemoveAt(index); | |
} | |
BaseAdd(index, value); | |
} | |
} | |
protected override ConfigurationElement CreateNewElement() | |
{ | |
return new BlogCategoryConfigurationElement(); | |
} | |
protected override object GetElementKey(ConfigurationElement element) | |
{ | |
return ((BlogCategoryConfigurationElement)element).Id; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment