Returns 400 错误请求 POST,PUT 方法但 GET 工作正常

Returns 400 Bad Request by POST, PUT methods but GET is working okay

我正在尝试调用 POST/PUT(通过使用 HttpClient,在 Swagger 或 Postman 中有效)方法,但它是 return 400 状态代码(错误请求)。它检查数据库是否存在并且 return 没问题。

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
  Executed DbCommand (27ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
  SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
  Executed DbCommand (42ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

  IF EXISTS
      (SELECT *
       FROM [sys].[objects] o
       WHERE [o].[type] = 'U'
       AND [o].[is_ms_shipped] = 0
       AND NOT EXISTS (SELECT *
           FROM [sys].[extended_properties] AS [ep]
           WHERE [ep].[major_id] = [o].[object_id]
               AND [ep].[minor_id] = 0
               AND [ep].[class] = 1
               AND [ep].[name] = N'microsoft_database_tools_support'
      )
  )
  SELECT 1 ELSE SELECT 0

我也在使用这部分代码:

var user = new User()
        {
            UserName = "dasdas",
            PhoneNumber = "dfsdfsdf",
            FirstName = "fdsfsd",
            LastName = "fdsfsdfsdfsd"
        };
        var json = JsonConvert.SerializeObject(user);
        var data = new StringContent(json, Encoding.UTF8, "application/json");
        using var client = new HttpClient();
        string url = Client.BaseAddress + RequestType.User;
        var response = client.PostAsync(url, data).Result;

我尝试使用另一个 API 并且它有效,但在我的 API 中没有错误等等。

正如this document所说:

Beginning with .NET 6, new projects include the <Nullable>enable</Nullable> element in the project file. Once the feature is turned on, existing reference variable declarations become non-nullable reference types.

在 .NET 6 中,non-nullable 属性 必须是必需的,否则模型验证将失败。

为达到您的要求,您可以从项目文件中删除 <Nullable>enable</Nullable>