show-notice
hide-notice

Wednesday, 21 August 2013

How to store custom data in web.config.


Introduction:

Here i will explain How to store custom data in web.config?

Description:


Web.Config:


  
    
....
CS:
public class DataProviders : IConfigurationSectionHandler
{
    private static bool _initialized;
    public static List _providers;

    public object Create(object parent, object configContext, XmlNode section)
    {
        XmlNodeList providers = section.SelectNodes("Provider");

        _providers = new List();

        foreach (XmlNode provider in providers)
        {
            _providers.Add(new Provider
            {
                Type = provider.Attributes["type"].Value,
                Alias = provider.Attributes["alias"].Value,
            });
        }

        return null;
    }

    public static void Init()
    {
        if (!_initialized)
        {
            ConfigurationManager.GetSection("DataProviders");
            _initialized = true;
        }
    }

    public static IEnumerable GetData(string dataProviderAlias)
    {
        return _providers.Where(p => p.Alias == dataProviderAlias);
    }
}

public class Provider
{
    public string Type { get; set; }
    public string Alias { get; set; }
}
Global:
public class Global : System.Web.HttpApplication
{
    void Application_BeginRequest(object sender, EventArgs e)
    {
        DataProviders.Init();
    }
}

Usage:
var providers = DataProviders.GetData("FirstProvider").ToList();

SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com