Skip to content

Instantly share code, notes, and snippets.

@mike-ward
Last active December 16, 2015 15:09
Show Gist options
  • Save mike-ward/5453384 to your computer and use it in GitHub Desktop.
Save mike-ward/5453384 to your computer and use it in GitHub Desktop.
Custom Configuration Section for Web.config
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