Is it possible to pass application parameter in web.config to Common C # class

Is it possible to pass the "string" parameter of an application in the web.config file to the Common C # class?

+3
source share
3 answers

Of course, this is possible - but you need to keep in mind that a properly designed class (if it is clearly not intended for ASP.NET) should not know and not care about where the information comes from. There should be a property (or a method, but properties are more of a “.NET way” to do things) that you set with a string value from the application itself, instead of the class directly capturing information from web.config.

+8
source

ConfigurationManager.AppSettings["KeyToSetting"] web.config( app.config)

+10

, , , , .

, (, ) , - , , . , - .

public static class ApplicationConfiguration
{
    private static DateTime myEpoch;
    public static DateTime Epoch
    {
       get
       {
          if (myEpoch == null)
          {
              string startEpoch = ConfigurationManager.AppSettings["Epoch"];
              if (string.IsNullOrEmpty(startEpoch))
              {
                 myEpoch = new DateTime(1970,1,1);
              }
              else
              {
                 myEpoch = DateTime.Parse(startEpoch);
              }
          }
          return myEpoch;   
       }
    }
}
0

Source: https://habr.com/ru/post/1698419/


All Articles