aspにアップグレードNetcore 3.1で遭遇したjson異常

4123 ワード

発生したjson異常:重大度コード説明項目ファイル行禁止表示状態エラーCS 1061'「JsonOptions」には「SerializerSettings」の定義が含まれておらず、最初の「JsonOptions」タイプのパラメータを受け入れるアクセス可能な拡張方法「SerializerSettings」が見つかりません(using命令またはプログラムセット参照が欠けていますか?)
処理方法:
services.AddMvc().AddJsonOptions(options =>
            {
                //      
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //        key
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();               
            });

次のように変更します.
            services.AddMvc().AddNewtonsoftJson(options =>
            {
                //      
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //        key
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();             
            });

資料を調べる:https://docs.microsoft.com/en-us/aspnet/core/release-notes/aspnetcore-3.0?view=aspnetcore-3.1
中には、
New JSON serialization
ASP.NET Core 3.0 now uses System.Text.Json by default for JSON serialization:
  • Reads and writes JSON asynchronously.
  • Is optimized for UTF-8 text.
  • Typically higher performance than  Newtonsoft.Json .

  • To add Json.NET to ASP.NET Core 3.0, see Add Newtonsoft.Json-based JSON format support.
    この文章で解決策を見つけたhttps://stackoverflow.com/questions/55666826/where-did-imvcbuilder-addjsonoptions-go-in-net-core-3-0
    As part of ASP.NET Core 3.0, the team moved away from including Json.NET by default. You can read more about that in general in the announcement on breaking changes to Microsoft.AspNetCore.App.
    ASPとしてNET Core 3.0の一部で、チームはデフォルトでJsonを含まない.NET.MicrosoftについてAspNetCore.Appが重大な変更を行った公告には、この内容の詳細が記載されています.
    Instead of Json.NET, ASP.NET Core 3.0 and .NET Core 3.0 include a different JSON API that focuses a bit more on performance. You can learn about that more in the announcement about “The future of JSON in .NET Core 3.0”. Jsonの代わりにNET,ASP.NET Core 3.0と.NET Core 3.0は、パフォーマンスをより重視する異なるJSON APIを含む.詳細については、.NET Core 3.0のJSONの将来についてのお知らせを参照してください.
    The new templates for ASP.NET Core will no longer bundle with Json.NET but you can easily reconfigure the project to use it instead of the new JSON library. This is important for both compatibility with older projects and also because the new library is not supposed to be a full replacement, so you won't see the full feature set there.
    ASP.NET Coreの新しいテンプレートはJsonとはなりません.NETはバンドルされていますが、新しいJSONライブラリではなく、プロジェクトを簡単に再構成できます.これは、以前のプロジェクトとの互換性、および新しいライブラリの完全な代替が重要であるため、ここではすべての機能が表示されません.
    In order to reconfigure your ASP.NET Core 3.0 project with Json.NET, you will need to add a NuGet reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson, which is the package that includes all the necessary bits. Then, in the Startup’s ConfigureServices, you will need to configure MVC like this:
    Jsonを使うためにNET再構成ASP.NET Core 3.0プロジェクトは、Microsoftに追加する必要があります.AspNetCore.Mvc.NewtonsoftJsonのNuGetは、必要なすべてのビットを含むパッケージを参照しています.次に、以下に示すように、起動会社のコンフィギュレーションサービスでMVCを構成する必要があります.
    services.AddControllers()
        .AddNewtonsoftJson();

    This sets up MVC controllers and configures it to use Json.NET instead of that new API. Instead of controllers, you can also use a different MVC overload (e.g. for controllers with views, or Razor pages). That AddNewtonsoftJson method has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions in ASP.NET Core 2.x.
    これにより、MVCコントローラが設定され、Jsonを使用するように構成する.NETはこの新しいAPIではありません.コントローラに加えて、他のMVCリロード(ビューまたはRazorページを持つコントローラなど)も使用できます.このAddNewtonsoftJsonメソッドには、ASPのようにリロードすることができます.NET Core 2.xではAddJsonOptionsと同様にJsonを配置する.NETオプション.
    services.AddControllers()
        .AddNewtonsoftJson(options =>
        {
            options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        });

    暇を見つけて3.1のシステムを比較しなければならないようだ.text.jsonとnewton.json認証はもっと使いやすいです.