枚举类型不再适用于 .Net core 3.0 FromBody 请求对象
Enum type no longer working in .Net core 3.0 FromBody request object
我最近将我的网站 api 从 .Net core 2.2 升级到 .Net core 3.0,并注意到当我将 post 中的枚举传递给我的请求时,我的请求现在出现错误端点。例如:
我的 api 端点有以下模型:
public class SendFeedbackRequest
{
public FeedbackType Type { get; set; }
public string Message { get; set; }
}
FeedbackType 看起来像这样:
public enum FeedbackType
{
Comment,
Question
}
这是控制器方法:
[HttpPost]
public async Task<IActionResult> SendFeedbackAsync([FromBody]SendFeedbackRequest request)
{
var response = await _feedbackService.SendFeedbackAsync(request);
return Ok(response);
}
我将其作为 post 正文发送给控制器:
{
message: "Test"
type: "comment"
}
我现在收到以下错误 posting 到此端点:
The JSON value could not be converted to MyApp.Feedback.Enums.FeedbackType. Path: $.type | LineNumber: 0 | BytePositionInLine: 13."
这在 2.2 中工作并在 3.0 中开始错误。我看到有关 json 序列化器在 3.0 中发生变化的讨论,但不确定应该如何处理。
如果您使用内置的 JsonStringEnumConverter 并将其传递到 JsonSerializerOptions,则支持将枚举序列化为字符串已经存在:
https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonstringenumconverter?view=netcore-3.0
这是一个使用它的示例测试:
https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/tests/Serialization/ReadScenarioTests.cs#L17
从 3.0 版开始,.NET Core 不再默认使用第三方 Newtonsoft.Json
(Json.NET),而是使用新的内置 System.Text.Json
(STJ) 序列化程序- 它不像 Json.NET 那样功能丰富,当然也有自己的问题和学习曲线来获得预期的功能。
如果您想切换回以前默认使用 Newtonsoft.Json
,则必须执行以下操作:
安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet 包。
在ConfigureServices()
中添加对AddNewtonsoftJson()
的调用
public void ConfigureServices(IServiceCollection services) {
//...
services.AddControllers()
.AddNewtonsoftJson(); //<--
//...
}
对于那些正在寻找片段的人
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
}
我最近将我的网站 api 从 .Net core 2.2 升级到 .Net core 3.0,并注意到当我将 post 中的枚举传递给我的请求时,我的请求现在出现错误端点。例如:
我的 api 端点有以下模型:
public class SendFeedbackRequest
{
public FeedbackType Type { get; set; }
public string Message { get; set; }
}
FeedbackType 看起来像这样:
public enum FeedbackType
{
Comment,
Question
}
这是控制器方法:
[HttpPost]
public async Task<IActionResult> SendFeedbackAsync([FromBody]SendFeedbackRequest request)
{
var response = await _feedbackService.SendFeedbackAsync(request);
return Ok(response);
}
我将其作为 post 正文发送给控制器:
{
message: "Test"
type: "comment"
}
我现在收到以下错误 posting 到此端点:
The JSON value could not be converted to MyApp.Feedback.Enums.FeedbackType. Path: $.type | LineNumber: 0 | BytePositionInLine: 13."
这在 2.2 中工作并在 3.0 中开始错误。我看到有关 json 序列化器在 3.0 中发生变化的讨论,但不确定应该如何处理。
如果您使用内置的 JsonStringEnumConverter 并将其传递到 JsonSerializerOptions,则支持将枚举序列化为字符串已经存在: https://docs.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonstringenumconverter?view=netcore-3.0
这是一个使用它的示例测试: https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/tests/Serialization/ReadScenarioTests.cs#L17
从 3.0 版开始,.NET Core 不再默认使用第三方 Newtonsoft.Json
(Json.NET),而是使用新的内置 System.Text.Json
(STJ) 序列化程序- 它不像 Json.NET 那样功能丰富,当然也有自己的问题和学习曲线来获得预期的功能。
如果您想切换回以前默认使用 Newtonsoft.Json
,则必须执行以下操作:
安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet 包。
在
的调用ConfigureServices()
中添加对AddNewtonsoftJson()
public void ConfigureServices(IServiceCollection services) {
//...
services.AddControllers()
.AddNewtonsoftJson(); //<--
//...
}
对于那些正在寻找片段的人
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
}