从配置文件中读取修补的属性值?

时间:2017-10-23 20:40:35

标签: c# configuration sitecore config appsettings

我想阅读Sitecore.ItemWebApi.config中的修补属性,以确定我的网站是否启用了Sitecore Item API。我正在寻找的这个值是itemwebapi.mode,我想知道它是否设置为“关闭”#m;'或不。

<site name="mysite">
    <patch:attribute name="itemwebapi.mode">StandardSecurity</patch:attribute>
    <patch:attribute name="itemwebapi.access">ReadOnly</patch:attribute>
    <patch:attribute name="itemwebapi.allowanonymousaccess">false</patch:attribute>
</site>

我尝试获取ConfigurationManager.AppSettings["itemwebapi.mode"]但它返回null。我如何获得这个值?

2 个答案:

答案 0 :(得分:1)

我认为您需要使用 sitecore配置工厂来阅读配置资料。  (http://sitecore-community.github.io/docs/documentation/Sitecore%20Fundamentals/Sitecore%20Configuration%20Factory/#config_factory

尝试如下。如果没有以类似的方式进行实验。

var refObj = Sitecore.Configuration.Factory.CreateObject("site/patch:attribute", true) as itemwebapi.mode;

如果没用,请发表评论。我会删除它。

答案 1 :(得分:1)

您可以使用Properties访问者访问站点节点上的属性, 如果未设置该属性,则该值将为空。对于当前的Context网站:

string mode = Sitecore.Context.Site.Properties["itemwebapi.mode"];
string access = Sitecore.Context.Site.Properties["itemwebapi.access"];
string anon = Sitecore.Context.Site.Properties["itemwebapi.allowanonymousaccess"];

如果您在ItemWebApi请求中检查(从-/item/开始),那么您可以查看ItemWebApi.Context

if (Sitecore.ItemWebApi.Context.Current != null)
{
    Mode mode = Sitecore.ItemWebApi.Context.Current.Settings.Mode;
    AccessType access = Sitecore.ItemWebApi.Context.Current.Settings.Access;
    bool anon = Sitecore.ItemWebApi.Context.Current.Settings.AnonymousAcessAllowed;
}

这将为您提供使用枚举的强类型访问设置。不幸的是,RuntimeSettings()的重载构造函数被标记为内部,因此无法在正常的Web请求中自行更新,但可以检查代码并在需要时执行类似操作。