ASP.NET MVC

Reading A Key From the Web.Config

In this article, I will explain how we can read the configuration setting from the Web.Config or App.Config file. To read the configuration setting we need to add System.Configuration assembly reference.

 

There are a few ways to read configuration settings.

  1. To read a value from the appSettings section

public void Read()
{
  var author = ConfigurationManager.AppSettings["Author"];
}

2. Read the entire section from the Web.Config or App.Config

public void Read()
{
    Dictionary<string, string> settings = new Dictionary<string, string>();
    var authorSettings = ConfigurationManager.GetSection("AuthorSetting") as NameValueCollection;
    if (authorSettings.Count > 0)
    {
        foreach (var key in authorSettings.AllKeys)
        {
            settings.Add(key, authorSettings[key]);
        }
    }
}

Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

Share
Published by
Faisal Pathan

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago