如何在 Asp.net Core Web Api 中默认使用 Newtonsoft.Json?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42290811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:48:07  来源:igfitidea点击:

How to use Newtonsoft.Json as default in Asp.net Core Web Api?

jsonserializationasp.net-web-apiasp.net-corejson.net

提问by Super Coder

I am new to ASP.Net Web Api Core. I have been using ASP.Net MVCfor past few years and I always have written an ActionFilterand used JSON.Netfor Serializingdata into JSON. So, in that way I replaced Microsoft's JavaScript Serializer(which is slower than JSON.Net) with JSON.Net(it is claimed to be 400% faster).

我是ASP.Net Web Api Core 的新手。我一直在使用ASP.Net MVC过去的几年里,我一直都写了ActionFilter,并用于JSON.NetSerializing数据导入JSON。因此,通过这种方式,我用(据称快 400%)替换了 Microsoft 的JavaScript Serializer(比 慢)。JSON.NetJSON.Net

How to do all this in ASP.Net Web Api Core? Where to change the default formattor?

如何在ASP.Net Web Api Core 中完成所有这些?在哪里更改默认格式化程序

Note: Please feel free to ask if you have any questions.

注意:如果您有任何问题,请随时提问。

Thanks

谢谢

采纳答案by MikeBeaton

In .NET Core 3.0+ include the NuGet package Microsoft.AspNetCore.Mvc.NewtonsoftJsonand then replace

在 .NET Core 3.0+ 中包含 NuGet 包Microsoft.AspNetCore.Mvc.NewtonsoftJson,然后替换

services.AddControllers();

in ConfigureServiceswith

ConfigureServices

services.AddControllers().AddNewtonsoftJson();

This is a pre-release NuGet package in .NET Core 3.0 but a full release package in .NET Core 3.1.

这是 .NET Core 3.0 中的预发布 NuGet 包,但 .NET Core 3.1 中的完整发布包。

I came across this myself, but I've found that the same answer with some additional info is in this SO question and answer.

我自己遇到过这个问题,但我发现在这个 SO question and answer 中有一些附加信息的相同答案

Edit:As a useful update: code with the call to AddNewtonsoftJson()will compile and run even without installing the Microsoft.AspNetCore.Mvc.NewtonsoftJsonNuGet package. If you do that, it runs with bothconverters installed, but defaulting to the System.Text.Jsonconverter which you presumably don't want since you're reading this answer. So you must remember to install the NuGet packagefor this to work properly (andremember to re-install it if you ever clear down and redo you NuGet dependencies).

编辑:作为一个有用的更新:AddNewtonsoftJson()即使不安装Microsoft.AspNetCore.Mvc.NewtonsoftJsonNuGet 包,调用 的代码也会编译和运行。如果你这样做,它会在安装了两个转换器的情况下运行,但默认为System.Text.Json你可能不想要的转换器,因为你正在阅读这个答案。因此,您必须记住安装 NuGet 包以使其正常工作(如果您清除并重做 NuGet 依赖项请记住重新安装它)。

回答by Tseng

ASP.NET Core already uses JSON.NET as JavaScriptSerializerisn't implemented/ported to .NET Core.

ASP.NET Core 已经使用 JSON.NET,因为JavaScriptSerializer没有实现/移植到 .NET Core。

Microsoft.AspNetCore.Mvcdepends on Microsoft.AspNetCore.Formatter.Jsonwhich depends on Microsoft.AspNetCore.JsonPatch, which depends on Newtonsoft.Json(see source).

Microsoft.AspNetCore.Mvc取决于Microsoft.AspNetCore.Formatter.Json哪个取决于Microsoft.AspNetCore.JsonPatch,哪个取决于Newtonsoft.Json(参见源代码)。

Update

更新

This is only true for ASP.NET Core 1.0 to 2.2. ASP.NET Core 3.0 removes the dependency on JSON.NET and uses it's own JSON serializer.

这仅适用于 ASP.NET Core 1.0 到 2.2。ASP.NET Core 3.0 移除了对 JSON.NET 的依赖,并使用它自己的 JSON 序列化程序。

回答by Kieran

here is a code snippet to adjust the settings for a .net core application

这是调整 .net 核心应用程序设置的代码片段

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc()
        .AddJsonOptions(options => {
            // send back a ISO date
            var settings = options.SerializerSettings;
            settings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
            // dont mess with case of properties
            var resolver = options.SerializerSettings.ContractResolver as DefaultContractResolver;
            resolver.NamingStrategy = null;
        });
}

回答by Грозный

Just use Newtonsoft.Json Version=11.0.1 or higher

只需使用 Newtonsoft.Json Version=11.0.1 或更高版本