从sitecore中的web.config获取设置

Ami*_*rma 5 web-config sitecore sitecore6 sitecore-mvc sitecore8

我想从sitecore解决方案中的web.config文件获取全局设置,我在配置文件中写入设置并能够在showconfig中看到它的条目.当我试图获得它的价值时,却没有给出适当的价值.我的代码是这样的:

 var newsBodyTemplateID = Sitecore.Configuration.Settings.GetSetting("NewsBody");
Run Code Online (Sandbox Code Playgroud)

当我评估这个时,它给出了这样的信息: 在此输入图像描述

我在这里失踪的东西可以解决它.

Vla*_*giu 8

首先,我不建议在web.config中添加您的设置.如果要升级Sitecore,则必须手动合并web.config.

如果您仍想在web.config中添加设置,则需要具有以下内容:

 <configuration>

     .....
      <appSettings>
        <add key="YourSeetings" value="your value" />
         ...
        </appSettings>

     .....
      </configuration>
Run Code Online (Sandbox Code Playgroud)

从C#代码中您需要使用

ConfigurationManager.AppSettings["YourSeetings"]
Run Code Online (Sandbox Code Playgroud)

如果您在section/configuration/sitecore/settings上有设置,则需要使用C#代码:

Sitecore.Configuration.Settings.GetSetting("yoursettingsname");
Run Code Online (Sandbox Code Playgroud)

您的配置文件将如下所示:

 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>

    <!-- General settings -->
    <settings>
        <setting name="YourSettingsFieldName" value="{1EPR25B2-98C6-45BF-B9E4-824ECAAEF499}" />
    </settings>
  </sitecore>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • OP表示他使用的是Sitecore设置,而不是AppSetting.这不回答这个问题.如果是AppSetting,他将不会在ShowConfig页面上看到它 (3认同)

Mar*_*ies 3

该方法将从节点返回设置Sitecore\Settings。还有另一种方法可以获得AppSettings.

Sitecore.Configuration.Settings.GetAppSetting()
Run Code Online (Sandbox Code Playgroud)

  • OP 说他使用的是 Sitecore 设置而不是 AppSetting。这并不能回答问题。如果它是一个 AppSetting,他就不会在 ShowConfig 页面上看到它 (2认同)