从 Web.Config 读取变量

问题描述 投票:0回答:6

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

c# .net asp.net web-config
6个回答
161
投票

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

78
投票

我建议你不要修改你的web.config,因为每次更改时,它都会重新启动你的应用程序。

但是您可以使用

System.Configuration.ConfigurationManager.AppSettings

读取 web.config

16
投票

如果您需要基础知识,您可以通过以下方式访问密钥:

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

7
投票

假设密钥包含在

<appSettings>
节点内:

ConfigurationSettings.AppSettings["theKey"];

至于“写作”——简单来说,不要。

web.config 不是为此设计的,如果您要不断更改某个值,请将其放入静态帮助器类中。


3
投票

Ryan Farley 在他的博客中发表了一篇关于此的精彩文章,其中包括为什么不写回 web.config 文件的所有原因: 写入 .NET 应用程序的配置文件


0
投票

我是 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");
© www.soinside.com 2019 - 2024. All rights reserved.