如何在 Orchard 主题中使用 c# singleton class

How to use c# singleton class in Orchard Themes

我正在使用 Orchard 来构建我的网站,并希望在主题中为生产环境和测试环境添加不同的元素。我尝试在 Orchard Themes class 中使用 c# 单例 class 来判断它是否是生产环境。然后在Themes中的cshtml文件中使用单例class。代码可以编译。但是在运行的时候,会抛出异常saying:The name 'Themes' does not exist in the current context。我必须在我添加的 class 之前添加 "Themes" 命名空间。否则,它不会通过编译。

详情: 我添加了一个单例 class: EnvironmentDev.cs 文件到 Themes.csproj 文件中。它包含静态 GetInstance 方法和 IsProd 属性。 在 Themes.csproj 文件中,它显示:

<ItemGroup>
    <Compile Include="EnvironmentDev.cs" />
</ItemGroup>

在主题的 Document.cshtml 文件中,我添加了以下代码:

@{
    if (Themes.EnvironmentDev.GetInstance().IsProd) {
        // Add production element
    }
    else {
        // Add test site element
    }
}

详细错误信息: 说明:编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。

编译器错误消息:CS0103:名称 'Themes' 在当前上下文中不存在

来源错误:

Line 46:     @Display(Model.Head)
Line 47:     @{
Line 48:         if (Themes.EnvironmentDev.GetInstance().IsProd) {
Line 49:             <script src="https://XXXX.js"></script> //using XXXX instead of real link
Line 50:         }

如有任何建议,我们将不胜感激。

我认为您需要使用以下命令为您的主题创建一个专用项目

codegen theme MyTheme /CreateProject:true /IncludeInSolution:true

否则将找不到您的代码。我对资源文件有同样的问题。

有关详细信息,请参阅 docs 中的 生成主题结构 段。