日本語版
最新ニュース
企業

Contを使用してヘッドレス実装の不要なプロパティを削除する

ヘッドレスで作業しているときに、フロントエンドにデータを送信するたびに、望まない多くのプロパティも JSON で表示され、大きくて読みにくい応答が作成されます。 これをリファクタリングするには、すべての呼び出しの前後に実行されるフィルターを作成します。 例:- 事前シリアル化コンテンツフィルター このクラスは、シリアル化の前に特定のプロパティのシリアル化を防止します。 そのサービスをシングルトンとして登録する internal class PreSerializationContentFilter : ContentFilter { public override void Filter(IContent content, ConverterContext converterContext) { content.Property.Remove("TopContentArea"); content.Property.Remove("MainContentArea"); content.Property.Remove("BottomContentArea"); content.Property.Remove("MetaTitle"); content.Property.Remove("MetaKeywords"); content.Property.Remove("MetaDescription"); content.Property.Remove("DisableIndexing"); content.Property.Remove("EnableNoFollow"); content.Property.Remove("OgContentType"); content.Property.Remove("Categories"); } } ポストシリアル化 API モデル フィルター…

Contを使用してヘッドレス実装の不要なプロパティを削除する

1725505495
2024-09-04 14:45:50

ヘッドレスで作業しているときに、フロントエンドにデータを送信するたびに、望まない多くのプロパティも JSON で表示され、大きくて読みにくい応答が作成されます。

これをリファクタリングするには、すべての呼び出しの前後に実行されるフィルターを作成します。

例:-

事前シリアル化コンテンツフィルター

  • このクラスは、シリアル化の前に特定のプロパティのシリアル化を防止します。
  • そのサービスをシングルトンとして登録する
internal class PreSerializationContentFilter  : ContentFilter
{
    public override void Filter(IContent content, ConverterContext converterContext)
    {
        content.Property.Remove("TopContentArea");
        content.Property.Remove("MainContentArea");
        content.Property.Remove("BottomContentArea");

        content.Property.Remove("MetaTitle");
        content.Property.Remove("MetaKeywords");
        content.Property.Remove("MetaDescription");
        content.Property.Remove("DisableIndexing");
        content.Property.Remove("EnableNoFollow");
        content.Property.Remove("OgContentType");
        content.Property.Remove("Categories");
    }
}

ポストシリアル化 API モデル フィルター

  • この汎用クラスは、リクエストを送信する前にシリアル化後にContentApiModelにプロパティを追加します。
  • そのサービスをシングルトンとして登録する
 public class PostSerializationApiModelFilter : ContentApiModelFilter
 {
     public override void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
     {
         try
         {
             // Set those values below as null, and configure ContentApiOption.IncludeNullValues = false in Initialization
             // then, response data will not include those ones.
             contentApiModel.ContentLink = null;
             contentApiModel.Language = null;
             contentApiModel.ExistingLanguages = null;
             contentApiModel.MasterLanguage = null;
             contentApiModel.ParentLink = null;
             contentApiModel.StartPublish = null;
             contentApiModel.StopPublish = null;
             contentApiModel.RouteSegment = null;
             contentApiModel.Changed = null;
             contentApiModel.Created = null;
             contentApiModel.Saved = null;
             contentApiModel.Status = null;
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }
     }
 }

これらのクラスを使用すると、不要なプロパティを削除できます。

お役に立てれば幸いです!! 🙂

2024年9月4日

#Contを使用してヘッドレス実装の不要なプロパティを削除する

執筆者について: nipponese

Nipponese News編集部は、国内外のニュースを日本語で分かりやすくお届けします。