解决 NET6 GET请求不设置参数值报错问题

1、调用的方法
 1         /// <summary>
 2         /// 获取干预集合
 3         /// </summary>
 4         /// <returns></returns>
 5         [HttpGet]
 6         public JsonResult GetIntervenes(string keyword)
 7         {
 8 
 9             List<OPNodeItem> result = null;
10 
11             return new JsonResult(result);
12         }

2、参数keyword不输入任何值,执行调用

3、报错信息

 1 {
 2   "errors": {
 3     "keyword": [
 4       "The keyword field is required."
 5     ]
 6   },
 7   "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
 8   "title": "One or more validation errors occurred.",
 9   "status": 400,
10   "traceId": "00-7ed4f9a50605c93c6fec504f1589cd83-31440a115974b86a-00"
11 }

4、解决方案

Program.cs文件中添加代码

 1 builder.Services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true); 

 

参考地址:https://blog.csdn.net/lindexi_gd/article/details/109294064

                  https://learn.microsoft.com/zh-cn/dotnet/api/microsoft.aspnetcore.mvc.mvcoptions.suppressimplicitrequiredattributefornonnullablereferencetypes?view=aspnetcore-7.0

 

posted @ 2023-04-26 10:14  Fast & Furious  阅读(260)  评论(0)    收藏  举报