簡體   English   中英

從Web.Config讀取變量

[英]Read Variable from Web.Config

如何添加和讀取web.config文件中的值?

給出以下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"];

我建議你不要修改web.config,因為每次更改時,它都會重啟你的應用程序。

但是,您可以使用System.Configuration.ConfigurationManager.AppSettings讀取web.config

如果您需要基礎知識,可以通過以下方式訪問密鑰:

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

要訪問我的Web配置鍵,我總是在我的應用程序中創建一個靜態類。 這意味着我可以在任何需要的地方訪問它們,而且我沒有在我的應用程序中使用字符串(如果它在Web配置中發生變化,我必須經歷所有更改它們的事件)。 這是一個示例:

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"];

至於“寫作” - 簡單地說, 不要。

web.config不是為此而設計的,如果您要不斷更改值,請將其放在靜態助手類中。

Ryan Farley在他的博客中發表了一篇很好的帖子,其中包括為什么不回寫web.config文件的所有原因: 寫入.NET應用程序的配置文件

我是siteConfiguration類,用於以這種方式調用我的所有appSetting。 如果能幫助任何人,我會分享它。

在“web.config”中添加以下代碼

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

現在,您可以定義一個類來獲取所有appSetting值。 像這樣

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;
        }
      }
   }
}

現在添加上一個類的引用並訪問像下面這樣的鍵調用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM