赞
踩
一般我们写REST的输入和输出的时候,可以定义一些DTO类来进行转换,但是某些时候,输入和返回的类的具体格式我们不能确定下来,这个时候,就需要直接使用Json字符串或者对象了。
但是如果直接返回JObject,会提示错误。这个时候,我们可以这样解决。
services.AddMvc().AddNewtonsoftJson();
其实这部分的内容在官方说明中有提到。
https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-3.1#add-newtonsoftjson-based-json-format-support
Prior to ASP.NET Core 3.0, the default used JSON formatters implemented using the Newtonsoft.Json package. In ASP.NET Core 3.0 or later, the default JSON formatters are based on System.Text.Json. Support for Newtonsoft.Json based formatters and features is available by installing the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package and configuring it in
Startup.ConfigureServices.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddNewtonsoftJson();
}
https://stackoverflow.com/questions/58072703/jsonresultobject-causes-the-collection-type-newtonsoft-json-linq-jtoken-is
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。