如何使用.NET 6.0中的最小API配置Newtonsoftjson
我有 net6.0
带有最小API的项目,我想使用 netwtonsoftjson
而不是内置 system.text.text.json
用于序列化和序列化的库避免。
目前,我对 jsonoptions
进行了此
builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.SerializerOptions.WriteIndented = true;
options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});
配置行为。相反,它看起来使用默认 system.text.json
配置。
builder.Services.Configure<JsonSerializerSettings>(options =>
{
options.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.Converters.Add(
new StringEnumConverter
{
NamingStrategy = new Newtonsoft.Json.Serialization.CamelCaseNamingStrategy()
});
});
在 net5.0
中,我知道我可以使用它
services.AddControllers().AddNewtonsoftJson((options) => //options); // OR
services.AddMvc().AddNewtonsoftJson((options) => //options);
,但是,如果我在 net6.0
项目中使用上述内容,那么我不再使用MinimAlapi?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如 docs :
,因此我建议仅使用
system.text.json
。但是,如果真的需要 - 您可以尝试一些解决方法。根据我的理解,最小的API依赖于有关类型结合的一些惯例。从我可以看到他们搜索下一个签名的方法 -
valuetask&lt; tmodel?&gt; bindasync(httpcontext上下文,参数参数) href =“ https://github.com/dotnet/aspnetcore/blob/a450cb69b5e4549f5551515cdb05771f568771f56cefd7/src/src/http/http/httppp.extp.extensionsionsion noreferrer“>
httpcontext.request.request.readfromjsonasync
内部使用system.text.json
,并且无法更改,因此services.add。 。
要使用
newtonsoft.json
您可以尝试下一个(除了通过app.mappost直接处理请求(“/pst”,(httpcontext c)=&gt; c.request ...)< /code>):
如果您对所有需要使用它的类进行控制
”
注意
basemodel&lt; tmodel&gt;
在此示例中插入非常幼稚,并且可以改善(请查看httprequestjsonextensions.readfromjsonasync
至少)。如果您无法控制模型或不想从某些基础继承它们,则可以考虑创建包装器:
使用使用情况
:
services.addcontrollers()
As mentioned in the docs:
So I would recommend to just use
System.Text.Json
. But if really needed - there are workarounds you can try.From my understanding Minimal APIs rely on some conventions regarding type binding. From what I can see they search for method with next signature -
ValueTask<TModel?> BindAsync(HttpContext context, ParameterInfo parameter)
on the type otherwise will try to usehttpContext.Request.ReadFromJsonAsync
which internally usesSystem.Text.Json
and that can't be changed, soservices.Add...().AddNewtonsoftJson((options) => //options);
approach will not work.To use
Newtonsoft.Json
you can try next (other than directly handling request viaapp.MapPost("/pst", (HttpContext c) => c.Request...)
):If you have control over all your classes which needs to be deserialized using it you can inherit them all from some generic base class which will have the method with needed signature (also you can use interface with implemented static method):
And usage:
Note that
BaseModel<TModel>
implemenation in this example is quite naive and possibly can be improved (check outHttpRequestJsonExtensions.ReadFromJsonAsync
at least).If you don't have control over the models or don't want to inherit them from some base you can look into creating wrappers:
And usage changes to:
Some extra useful links:
services.AddControllers().AddNewtonsoftJson((options) => //options);