Handling configuration settings in asp.net web application

后端 未结 4 520
生来不讨喜
生来不讨喜 2021-01-14 07:42

I have a web application with over 200 configuration settings. These control everything from UI to Business logic.

Should these be retrieved on application startup o

4条回答
  •  死守一世寂寞
    2021-01-14 08:22

    I agree with Kelsey, don't overcomplicate things and just use the .NET framework classes for retrieving the values. Web.config is usually where you'd keep them.
    I would however recommend creating a simple static class for access:

    using System.Configuration // Add a reference for this... ;
    
    public static class Config
    {
      public static string DbConnection
      {
        get
        {
          return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        }
      }
    
      public static string SomeValue
      {
        get 
        {
          return ConfigurationManager.AppSettings["SomeValue"].ToString();
        }
      }
    }
    

    It'll give you strongly typed values and easy access. You can also perform some kind of validation with a class like this. Very handy. And if you later decide not to use web.config, this class is easily changed to retrieve the values elsewhere.

提交回复
热议问题