从外部代码,如何在没有模块的情况下访问 DynamicCode?

问题描述

我在自定义 2sxc 应用程序中有一个 Content-Type (AppId = 10)。内容类型名称是“SeasonSettings”,其中包含用于每年修改网站主题几次的颜色、字体、图标和图像。没有视图或模块,客户端通过进入应用程序、数据选项卡等的管理来获取设置。

当我尝试从 Home.ascx 中我的皮肤文件的 C# 代码访问它时,我有以下内容

// the details you need to kNow
var appId = 7;                     // The AppId that of the target Content Type
string ctName = "SeasonSettings";  // The name of the Content Type
int entityId = 2345;               // The EntityId of the item with our settings
 
// get a handle to the App
var app7 = ToSic.Sxc.Dnn.Factory.App(appId);
 

现在我卡住了。因为我没有模块,所以我无法获得 CmsBlock (BlockBuilder),我需要获得 DynamicCode 以便我可以使用 AsList、AsEntity、AsDynamic 等。

所以我接下来的步骤是这样的:

// this fails because "AsList" is unkNown
var setting = AsList(app7.Data[ctName])
  .Where(e => e.EntityId == entityId)
  .First()
;
 
<p>Color: @setting.BgColor</p>

无需在页面上放置模块/视图并使用 CmsBlock(tabId,moduleId) 等,是否有另一种方法app7 获得 DynamicCode 或调用/使用 AsList() 的任何方法

解决方法

到目前为止,我认为没有一种简单的方法可以做到这一点,因为动态代码旨在用于 razor 文件和自定义 API。

我建议你在初始化这些东西的地方快速创建一个隐藏模块,然后获取而不是阻塞以供进一步使用。