I am extracting the value in the app.config for the key of keyUri
and see this syntax used by MS , as well as having used this syntax as well when doing connection strings. In the below snippet ( System.Configuration
has been added, and the DLL has been added) of C#, I am getting this error:
C#
/// Error on the .Value
string keyUri = ConfigurationManager.AppSettings["keyUri"].Value;
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="keyUri" value="test" />
</appSettings>
</configuration>
Based on the code, it should be looking within the <appSettings>
, and returning the value for the key keyUri
. Instead, I get an error stating,
"String Does Not Contain Definition For Value"
The ConfigurationManager.AppSettings
stores the data as KeyValuePair, so by specifying the key you will get the value. SO its fine to use like this:
string keyUri = ConfigurationManager.AppSettings["keyUri"]
Which means ConfigurationManager.AppSettings["keyUri"]
will return a string, when you append the .Value
to this line, you are asking the compiler to get the value property of the string, hence it says "String Does Not Contain Definition For Value" . By using the above code you will get the required value in the string variable keyUri
.
string keyUri = ConfigurationManager.AppSettings["keyUri"];
这会给你结果
ConfigurationManager.AppSettings
is a NameValueCollection
. When you access it using the index , it returns a string
. string
doesn't have a property Value
, in this case the returned string is the value.
So simply omit .Value
:
string keyUri = ConfigurationManager.AppSettings["keyUri"];
It's Obvious that there is no property Value
as other have already mentioned. So you should access it like
string keyUri = ConfigurationManager.AppSettings["keyUri"];
But I think you meant to call the GetValues()
method like below cause ConfigurationManager.AppSettings
returns a NameValueCollection
string keyUri = ConfigurationManager.AppSettings.GetValues("keyUri");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.