モデルバインディング

Version: 22.x
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

大事な

September 2024より前にASP.NET Coreアプリを統合した場合は、従来の ASP.NET CoreレンダリングSDKバージョン22以前を使用しています。このSDKは更新プログラムを受け取らなくなったため、新しい ASP.NET Core SDKの最新バージョンにアップグレードすることをお勧めします。

Sitecoreレンダリング エンジンはASP.NETレイアウト サービスによってSitecoreLayoutResponseオブジェクトで返される主要なオブジェクト、プロパティ、およびフィールドに対するモデル バインディングのサポートを提供します。

モデルのバインドはIFieldReaderで行われ、大文字と小文字が区別されます。モデル プロパティが属性を介してマッピングされていない場合は、デフォルトのASP.NET Model-View-Controllerモデル バインディング動作が引き継ぎます。

属性、ソース、プロバイダなどは、レンダリング エンジン ライブラリのSitecore.AspNet.RenderingEngine.Binding名前空間にあります。

IFieldを実装するモデル プロパティは、明示的な属性を必要とせず、同じ名前の現在のコンポーネントまたは現在のルート フィールドにバインドされます。

currentコンポーネント、レスポンス、またはルートは、HttpContextオブジェクトのコンテキスト情報として使用可能なSitecoreLayoutResponseインスタンス内の対応するオブジェクトを参照します。特定のリクエストに対して、現在のレスポンスとルートはすべてのコンポーネントで同じですが、placeholderタグヘルパーは現在のコンポーネントを動的に設定します。

レイアウト サービスの応答の例を次に示します。

RequestResponse
{
  "sitecore": {
    "context": {                               // context
      "contextproperty1": "",                  // context property
      "contextproperty2": "",                  // context property
      "CustomContextSingleProperty": "",       // custom single context property
      "CustomContextClass": {
        "customcontextprop": "",               // custom context property of custom context class
      },
    },
    "route": {                                 // route
      "routeproperty1": "",                    // route property
      "routeproperty2": "",                    // route property
      "fields": {                              // route fields
        "routefield1": {                       // route field
          "value": ""
        }
      },
      "placeholders": {                        // route placeholders
        "placeholder1": [                      // route placeholder
          {
            "componentproperty1": "",          // component property
            "componentproperty2": "",          // component property
            "fields": {                        // component fields
              "componentfield1": {             // component field
                "value": ""
              }
            }
          }
          ]
        }
    }
  }
}

一致するビュー モデル クラス:

RequestResponse
using Sitecore.AspNet.RenderingEngine.Binding;
using Sitecore.AspNet.RenderingEngine.Binding.Attributes;
using Sitecore.LayoutService.Client.Response;
using Sitecore.LayoutService.Client.Response.Model;
using Sitecore.LayoutService.Client.Response.Model.Fields;

public class ComplexComponent
{
  public SitecoreLayoutResponse Response {get; set;}

  public Context Context {get; set;}                            // Bind to "context"

  [SitecoreContextProperty(Name = "contextproperty1")]          // Bind to "contextproperty1". Also binds custom Sitecore "Context" properties.
  public bool ContextProperty1 { get; set; }

  [SitecoreContextProperty(Name = "contextproperty2")]          // Bind to "contextproperty2". Also binds custom Sitecore "Context" properties.
  public bool ContextProperty2 { get; set; }
  
  public ContextExt ContextExt { get; set; }                    // Bind to current Sitecore "Context" and all custom context extensions. Inherits "Context" class.

  [SitecoreContext]                                             // Bind to current Sitecore "Context" and Custom Sitecore Context Properties and objects.
  public CustomContextClass CustomContextClass { get; set; } 

  public Route Route {get; set;}                                // Bind to "route"

  [SitecoreRouteProperty(Name = "routeproperty1")]              // Bind to "routeproperty1"
  public string RouteProperty1 { get; set; }

  [SitecoreRouteProperty(Name = "routeproperty2")]              // Bind to "routeproperty2"
  public string RouteProperty2 { get; set; }

  [SitecoreRouteFields]                                         // Bind to "route"."fields"
  public RouteFields RouteFields { get; set; }

  [SitecoreRouteField(Name = "routefield1")]                    // Bind to "routefield1"
  public TextField RouteField1 { get; set; }
  

  [SitecoreComponentProperty(Name = "componentproperty1")]      // Bind to "componentproperty1"
  public string ComponentProperty1 { get; set; }

  [SitecoreComponentProperty(Name = "componentproperty2")]      // Bind to "componentproperty2"
  public string ComponentProperty2 { get; set; }

  [SitecoreComponentFields]                                     // Bind to "route"."placeholders"
  public ComponentFields ComponentFields { get; set; }

  [SitecoreComponentField(Name = "componentfield1")]            // Bind to "componentfield1"
  public TextField ComponentField1 { get; set; }
}

public class RouteFields
{
  [SitecoreRouteField(Name = "routefield1")]                    // Bind to "routefield1"
  public TextField RouteField1 { get; set; }
}

public class ComponentFields
{
  [SitecoreComponentField(Name = "componentfield1")]            // Bind to "componentfield1"
  public TextField ComponentField1 { get; set; }
}

// A class to extend the current Sitecore "Context" with custom properties
public class ContextExt:Context
{
  public CustomContextClass CustomContextClass { get; set; }
  public string CustomContextSingleProperty { get; set; } 
}

// A class for custom Context properties
public class CustomContextClass
{
  public string CustomContextProp { get; set; }
}
メモ

コンテキスト拡張の例については、「 レイアウト サービスによって返されるコンテキスト データの拡張」を参照してください。

何かフィードバックはありますか?

この記事を改善するための提案がある場合は、