缺少部分声明
missing a section declaration
我在 Web.config
中完成了以下操作
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" allowLocation="true" />
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
... etc
然后在 Views/Web.config 中我试过这样使用它
<secureAppSettings>
<!--Captcha keys-->
<add key="RecaptchaPrivateKey" value="somevalue" />
<add key="RecaptchaPublicKey" value="someothervalue" />
</secureAppSettings>
然而,每次尝试打开网站时,我都会收到以下错误消息:
Config Error The configuration section 'secureAppSettings' cannot
be read because it is missing a section declaration
而且我不明白我应该做什么。我目前正在尝试在这样的视图中使用它:
@((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["RecaptchaPublicKey"]
编辑:
我已经尝试过,因为评论之一建议将 configSections 移动到 View/web.config
结果是下面的错误信息
Parser Error Message: An error occurred while creating the handler for the secureAppSettings configuration section: The specified assembly name or specified codebase was invalid. (Exception from HRESULT: 0x80131047)
我认为那是我做错了的标志,但我现在明白,也许我需要在这里修复一些东西,而不是用我以前做的其他方式来做。
然而,对我来说,信息和以前一样神秘..
说明我要存档的内容。
我的 Web.config 中有一些我想要加密的敏感信息。
我读到我可以制作一个 secureAppSettings 部分,然后从那里对其进行加密。我想将信息从 appSettings 部分中分离出来的原因是因为我在其中有很多其他信息,我真的不需要加密。
最终所有这一切都应该导致我能够制作一个 MSI 网站安装程序,在安装过程中,我可以在加密 secureAppSettings 之前更改或添加设置到 appSettings 和 secureAppSettings 部分。以便轻松设置和安装我正在构建的网站。
现在我不确定我的解决方案目前是否可行,但如果不行,那么我想指出正确的方向,以解决我的最终目标,即能够制作 MSI 安装程序,我可以在安装期间更改配置文件并加密任何敏感信息。
在回复我的评论时,您提到您还尝试使用根 web.config
文件而不是 Views
文件夹中的文件进行设置。
理论上应该可以使用Views\web.config
文件。
使用 ASP.NET MVC
我只能使用根 web.config
.
成功
这看起来像下面这样。
请注意,我使用了一个不太明确的 section
定义。
Web.config
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<!-- Other configuration elements -->
<secureAppSettings>
<!--Captcha keys-->
<add key="RecaptchaPrivateKey" value="somevalue" />
<add key="RecaptchaPublicKey" value="someothervalue" />
</secureAppSettings>
</configuration>
在我看来
<span>@(((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["RecaptchaPublicKey"])</span>
我在 Web.config
中完成了以下操作 <configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" allowLocation="true" />
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
... etc
然后在 Views/Web.config 中我试过这样使用它
<secureAppSettings>
<!--Captcha keys-->
<add key="RecaptchaPrivateKey" value="somevalue" />
<add key="RecaptchaPublicKey" value="someothervalue" />
</secureAppSettings>
然而,每次尝试打开网站时,我都会收到以下错误消息:
Config Error The configuration section 'secureAppSettings' cannot be read because it is missing a section declaration
而且我不明白我应该做什么。我目前正在尝试在这样的视图中使用它:
@((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["RecaptchaPublicKey"]
编辑:
我已经尝试过,因为评论之一建议将 configSections 移动到 View/web.config
结果是下面的错误信息
Parser Error Message: An error occurred while creating the handler for the secureAppSettings configuration section: The specified assembly name or specified codebase was invalid. (Exception from HRESULT: 0x80131047)
我认为那是我做错了的标志,但我现在明白,也许我需要在这里修复一些东西,而不是用我以前做的其他方式来做。
然而,对我来说,信息和以前一样神秘..
说明我要存档的内容。
我的 Web.config 中有一些我想要加密的敏感信息。 我读到我可以制作一个 secureAppSettings 部分,然后从那里对其进行加密。我想将信息从 appSettings 部分中分离出来的原因是因为我在其中有很多其他信息,我真的不需要加密。 最终所有这一切都应该导致我能够制作一个 MSI 网站安装程序,在安装过程中,我可以在加密 secureAppSettings 之前更改或添加设置到 appSettings 和 secureAppSettings 部分。以便轻松设置和安装我正在构建的网站。
现在我不确定我的解决方案目前是否可行,但如果不行,那么我想指出正确的方向,以解决我的最终目标,即能够制作 MSI 安装程序,我可以在安装期间更改配置文件并加密任何敏感信息。
在回复我的评论时,您提到您还尝试使用根 web.config
文件而不是 Views
文件夹中的文件进行设置。
理论上应该可以使用Views\web.config
文件。
使用 ASP.NET MVC
我只能使用根 web.config
.
成功
这看起来像下面这样。
请注意,我使用了一个不太明确的 section
定义。
Web.config
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<!-- Other configuration elements -->
<secureAppSettings>
<!--Captcha keys-->
<add key="RecaptchaPrivateKey" value="somevalue" />
<add key="RecaptchaPublicKey" value="someothervalue" />
</secureAppSettings>
</configuration>
在我看来
<span>@(((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["RecaptchaPublicKey"])</span>