developer tip

Web.Config에서 변수 읽기

optionbox 2020. 9. 11. 07:55
반응형

Web.Config에서 변수 읽기


web.config 파일 에서 값을 어떻게 추가하고 읽을 수 있습니까?


변경할 때마다 응용 프로그램을 다시 시작하기 때문에 web.config를 수정하지 않는 것이 좋습니다.

그러나 다음을 사용하여 web.config를 읽을 수 있습니다. System.Configuration.ConfigurationManager.AppSettings


다음 web.config가 주어지면 :

<appSettings>
     <add key="ClientId" value="127605460617602"/>
     <add key="RedirectUrl" value="http://localhost:49548/Redirect.aspx"/>
</appSettings>

사용 예 :

using System.Configuration;

string clientId = ConfigurationManager.AppSettings["ClientId"];
string redirectUrl = ConfigurationManager.AppSettings["RedirectUrl"];

기본 사항을 원하면 다음을 통해 키에 액세스 할 수 있습니다.

string myKey = System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString();
string imageFolder = System.Configuration.ConfigurationManager.AppSettings["imageFolder"].ToString();

내 웹 구성 키에 액세스하기 위해 항상 내 응용 프로그램에서 정적 클래스를 만듭니다. 즉, 필요한 곳에서 액세스 할 수 있고 내 애플리케이션 전체에서 문자열을 사용하지 않는다는 의미입니다 (웹 구성에서 변경되는 경우 모든 변경 작업을 거쳐야 함). 다음은 샘플입니다.

using System.Configuration;

public static class AppSettingsGet
{    
    public static string myKey
    {
        get { return ConfigurationManager.AppSettings["myKey"].ToString(); }
    }

    public static string imageFolder
    {
        get { return ConfigurationManager.AppSettings["imageFolder"].ToString(); }
    }

    // I also get my connection string from here
    public static string ConnectionString
    {
       get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; }
    }
}

키가 <appSettings>노드 내부에 포함되어 있다고 가정합니다 .

ConfigurationSettings.AppSettings["theKey"];

"쓰기"에 관해서는 간단하게 말하면 안됩니다.

The web.config is not designed for that, if you're going to be changing a value constantly, put it in a static helper class.


Ryan Farley has a great post about this in his blog, including all the reasons why not to write back into web.config files: Writing to Your .NET Application's Config File


I am siteConfiguration class for calling all my appSetting like this way. I share it if it will help anyone.

add the following code at the "web.config"

<configuration>
   <configSections>
     <!-- some stuff omitted here -->
   </configSections>
   <appSettings>
      <add key="appKeyString" value="abc" />
      <add key="appKeyInt" value="123" />  
   </appSettings>
</configuration>

Now you can define a class for getting all your appSetting value. like this

using System; 
using System.Configuration;
namespace Configuration
{
   public static class SiteConfigurationReader
   {
      public static String appKeyString  //for string type value
      {
         get
         {
            return ConfigurationManager.AppSettings.Get("appKeyString");
         }
      }

      public static Int32 appKeyInt  //to get integer value
      {
         get
         {
            return ConfigurationManager.AppSettings.Get("appKeyInt").ToInteger(true);
         }
      }

      // you can also get the app setting by passing the key
      public static Int32 GetAppSettingsInteger(string keyName)
      {
          try
          {
            return Convert.ToInt32(ConfigurationManager.AppSettings.Get(keyName));
        }
        catch
        {
            return 0;
        }
      }
   }
}

Now add the reference of previous class and to access a key call like bellow

string appKeyStringVal= SiteConfigurationReader.appKeyString;
int appKeyIntVal= SiteConfigurationReader.appKeyInt;
int appKeyStringByPassingKey = SiteConfigurationReader.GetAppSettingsInteger("appKeyInt");

참고URL : https://stackoverflow.com/questions/3854777/read-variable-from-web-config

반응형