从网络上阅读密钥。配置使用ConfigurationManager

时间:2021-05-13 11:28:37

I am trying to read the keys from the Web.config file in a different layer than the web layer (Same solution)

我正在试着从网络上读取钥匙。与web层不同的层中的配置文件(相同的解决方案)

Here is what I am trying:

以下是我正在尝试的:

string userName = System.Configuration.ConfigurationManager.AppSettings["PFUserName"];
string password = System.Configuration.ConfigurationManager.AppSettings["PFPassWord"];

And here is my appSettings in the Web.config file:

这是我在网上的应用程序设置。配置文件:

<configuration>
   ....
   <appSettings>
      <add key="PFUserName" value="myusername"/>
      <add key="PFPassWord" value="mypassword"/>
   </appSettings>
   ....
</configuration>

When I debug the code username and password are just null, so it is not getting the value of the keys.

当我调试代码时,用户名和密码只是null,所以它没有得到键的值。

What am I doing wrong to read these values?

我读这些值有什么不对吗?

7 个解决方案

#1


377  

Try using the WebConfigurationManager class instead. For example:

尝试使用WebConfigurationManager类。例如:

string userName = WebConfigurationManager.AppSettings["PFUserName"]

#2


25  

  var url = ConfigurationManager.AppSettings["ServiceProviderUrl"];

#3


3  

If the caller is another project, you should write the config in caller project not the called one.

如果调用者是另一个项目,那么应该在调用者项目中编写配置,而不是调用者项目中编写配置。

#4


3  

I found this solution to be quite helpful. It uses C# 4.0 DynamicObject to wrap the ConfigurationManager. So instead of accessing values like this:

我发现这个解决方案很有帮助。它使用c# 4.0 DynamicObject包装ConfigurationManager。而不是像这样访问值:

 WebConfigurationManager.AppSettings["PFUserName"]

you access them as a property:

您将它们作为属性访问:

dynamic appSettings = new AppSettingsWrapper();
Console.WriteLine(appSettings.PFUserName);  

EDIT: Adding code snippet in case link becomes stale:

编辑:在案例链接中添加代码片段变得陈旧:

public class AppSettingsWrapper : DynamicObject
{
     private NameValueCollection _items;

    public AppSettingsWrapper()
    {
        _items = ConfigurationManager.AppSettings;
    }

     public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = _items[binder.Name];
        return result != null;
    }
}

#5


0  

This issue happens if this project is being used by another project. Make sure you copy the app setting keys to the parent project's app.config or web.config.

如果这个项目被另一个项目使用,这个问题就会发生。确保将应用程序设置键复制到父项目的app.config或web.config。

#6


0  

Also you can try this line to get string value from app.config file.

还可以尝试从app.config文件中获取字符串值。

var strName= ConfigurationManager.AppSettings["stringName"];

#7


-5  

Sorry I've not tested this but I think it's done like this:

对不起,我还没有测试过这个,但我认为它是这样做的:

var filemap = new System.Configuration.ExeConfigurationFileMap();            
System.Configuration.Configuration config =  System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(filemap, System.Configuration.ConfigurationUserLevel.None);

//usage: config.AppSettings["xxx"]

#1


377  

Try using the WebConfigurationManager class instead. For example:

尝试使用WebConfigurationManager类。例如:

string userName = WebConfigurationManager.AppSettings["PFUserName"]

#2


25  

  var url = ConfigurationManager.AppSettings["ServiceProviderUrl"];

#3


3  

If the caller is another project, you should write the config in caller project not the called one.

如果调用者是另一个项目,那么应该在调用者项目中编写配置,而不是调用者项目中编写配置。

#4


3  

I found this solution to be quite helpful. It uses C# 4.0 DynamicObject to wrap the ConfigurationManager. So instead of accessing values like this:

我发现这个解决方案很有帮助。它使用c# 4.0 DynamicObject包装ConfigurationManager。而不是像这样访问值:

 WebConfigurationManager.AppSettings["PFUserName"]

you access them as a property:

您将它们作为属性访问:

dynamic appSettings = new AppSettingsWrapper();
Console.WriteLine(appSettings.PFUserName);  

EDIT: Adding code snippet in case link becomes stale:

编辑:在案例链接中添加代码片段变得陈旧:

public class AppSettingsWrapper : DynamicObject
{
     private NameValueCollection _items;

    public AppSettingsWrapper()
    {
        _items = ConfigurationManager.AppSettings;
    }

     public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = _items[binder.Name];
        return result != null;
    }
}

#5


0  

This issue happens if this project is being used by another project. Make sure you copy the app setting keys to the parent project's app.config or web.config.

如果这个项目被另一个项目使用,这个问题就会发生。确保将应用程序设置键复制到父项目的app.config或web.config。

#6


0  

Also you can try this line to get string value from app.config file.

还可以尝试从app.config文件中获取字符串值。

var strName= ConfigurationManager.AppSettings["stringName"];

#7


-5  

Sorry I've not tested this but I think it's done like this:

对不起,我还没有测试过这个,但我认为它是这样做的:

var filemap = new System.Configuration.ExeConfigurationFileMap();            
System.Configuration.Configuration config =  System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(filemap, System.Configuration.ConfigurationUserLevel.None);

//usage: config.AppSettings["xxx"]