Orchard CMS:从外部数据源创建内容项

Orchard CMS: Create Content Items from External Data Source

在 Orchard CMS 中,我有一个从外部数据源提取数据并将数据加载到 Orchard 内容部分的服务。该部分有一个将其与标题部分焊接在一起的迁移,我有一条路线,以便我的控制器通过 URL:

我正在使用控制器通过 URL 访问该项目,就像博客部分控制器一样。但是我无法呈现我的角色...... 博客控制器与以下内容类似:

    var asset = _assetService.Get(1234);
    if (asset == null) return HttpNotFound();

    var model = _services.ContentManager.BuildDisplay(asset); 

    return new ShapeResult(this, model);

但是如果我这样做,'BuildDisplay' 方法会查找 asset.ContentItem 但这是空的,尽管我的部分是从 'ContentPart'.

派生的

我需要做什么才能显示我的数据?

如果我没理解错的话,您是想只显示一部分,而不是整个内容项。

要显示单个形状,您可以执行以下操作:

private readonly IAssetService _assetService;

public MyController(IShapeFactory shapeFactory, IAssetService assetService) {
    _assetService = assetService;
    Shape = shapeFactory;
}

public dynamic Shape { get; set; }

public ActionResult MyAction(int assetId) {
    var asset = _assetService.Get(1234);
    if (asset == null) return HttpNotFound();

    // the shape factory can call any shape (.cshtml) that is defined
    // this method assumes you have a view called SomeShapeName.cshtml
    var model = Shape.SomeShapeName(asset);

    return new ShapeResult(this, model);
}

!!注意: 这不会启动部件的(显示)驱动程序,它只是 returns 具有给定模型的 .cshtml

通过让我的部分派生自 ContentPart,我可以使用以下控制器方法:

private readonly IAssetService _assetService;
private readonly IOrchardServices _services;

public MyController(IShapeFactory shapeFactory, IAssetService assetService, IOrchardServices services) {
    _assetService = assetService;
    _services = services;
    Shape = shapeFactory;
}

public dynamic Shape { get; set; }

public ActionResult MyAction(int assetId) {
    var asset = _assetService.Get(1234);
    if (asset == null) return HttpNotFound();

    // this method assumes you have a view called Parts.Asset.cshtml (see the AssetPartDriver)
    var model = _services.ContentManager.New("Asset");
    var item = contentItem.As<AssetPart>();
    item.Populate(asset) // Method that just populates the service loaded object into the ContentPart

    return new ShapeResult(this, _services.ContentManager.BuildDisplay(item));
}

这将使用 'AssetPartDriver':

public class AssetPartDriver : ContentPartDriver<AssetPart>
    {
        protected override DriverResult Display(AssetPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_Asset", () => shapeHelper.Parts_Asset()); // Uses Parts.Asset.cshtml
        }
    }

并结合 'Placement.info' 文件在屏幕上呈现:

<Placement>
  <Match ContentType="Asset">
    <Match DisplayType="Detail">
      <Place Parts_Asset="Content"/>
    </Match>
  </Match>
</Placement>

迁移文件结合了我的网络服务部分和其他 Orchard 部分:

public class Migrations : DataMigrationImpl
    {
        public int Create()
        {
            ContentDefinitionManager.AlterTypeDefinition("Asset", cfg => cfg
                .WithPart("AssetPart")
                .WithPart("AutoroutePart", builder => builder
                    .WithSetting("AutorouteSettings.AllowCustomPattern", "True"))
                .Listable()
                .Securable()
                .Creatable(false));

            ContentDefinitionManager.AlterPartDefinition("AssetPart", part => part
                .WithDescription("A part that contains details of an individual Web Service loaded asset."));
            return 1;
        }
    }

这些附加部件尚未使用,但可以在创建期间填充并使用放置文件单独显示。 这是我想要实现的第一步!!