当前位置:   article > 正文

asp.netcore返回json对象_net cor返回文件对象

net cor返回文件对象

一般我们写REST的输入和输出的时候,可以定义一些DTO类来进行转换,但是某些时候,输入和返回的类的具体格式我们不能确定下来,这个时候,就需要直接使用Json字符串或者对象了。

但是如果直接返回JObject,会提示错误。这个时候,我们可以这样解决。

  • Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson
  • Update Startup.ConfigureServices to call AddNewtonsoftJson: 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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

ref

https://stackoverflow.com/questions/58072703/jsonresultobject-causes-the-collection-type-newtonsoft-json-linq-jtoken-is

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/550324
推荐阅读
相关标签
  

闽ICP备14008679号