Enum type no longer working in .Net core 3.0 FromBody request object

前端 未结 3 1405
暗喜
暗喜 2021-01-03 21:37

I have recently upgraded my web api from .Net core 2.2 to .Net core 3.0 and noticed that my requests are getting an error now when I pass an enum in a post to my endpoint.

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-03 22:04

    As of version 3.0, .NET Core no longer uses the third-party Newtonsoft.Json (Json.NET) by default but the new, built-in System.Text.Json (STJ) serializer - which is not as feature-rich as Json.NET, and of course has its own issues and learning curve to get the expected features.

    If you’d like to switch back to the previous default of using Newtonsoft.Json, then you'll have to do the following:

    1. Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package.

    2. In ConfigureServices() add a call to AddNewtonsoftJson()

    public void ConfigureServices(IServiceCollection services) {
        //...
    
        services.AddControllers()
            .AddNewtonsoftJson(); //<--
    
        //...
    }
    

提交回复
热议问题