我正在开发一个Asp.net MVC-5 Web应用程序,我目前正在web.config
下的<appsettings>
文件中存储令牌,如下所示: -
<appSettings>
.....
<add key="PManagerToken" value="^****" />
.....
</appSettings>
所以我想在自定义部分中移动它,因为我想加密它。所以我尝试了以下方法: -
<appSettings>
<section name="MyCustomSection">
<add key="PToken" value="****" />
.......
</section>
......
</appSettings>
但我收到以下错误: -
Unrecognized element 'section'.
第二个问题,目前我正在我的应用程序中访问PToken: -
System.Web.Configuration.WebConfigurationManager.AppSettings["PToken"];
所以,如果我设法将其移动到自定义部分,我将如何访问它? 谢谢
修改
我在web.config中添加了一些内容,如下所示: -
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="customAppSettingsGroup">
<section name="customAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</sectionGroup>
</configSections>
<customAppSettingsGroup>
<customAppSettings>
<add key="KeyOne" value="ValueOne"/>
<add key="KeyTwo" value="ValueTwo"/>
</customAppSettings>
</customAppSettingsGroup>
</configuration>
我能够在我的申请中引用它如下: -
NameValueCollection settings =
ConfigurationManager.GetSection("customAppSettingsGroup/customAppSettings")
as System.Collections.Specialized.NameValueCollection;
if (settings != null)
{
foreach (string key in settings.AllKeys)
{
Response.Write(key + ": " + settings[key] + "<br />");
}
}
这样会有效吗?似乎它在某种程度上很容易做,不像我收到的评论提到定义一个自己的部分需要额外的工作要做!!