简体   繁体   English

通过 POST、PUT 方法返回 400 错误请求,但 GET 工作正常

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

I am trying to call POST/PUT(by using HttpClient, in Swagger or Postman it works) method but it is return 400 status code(Bad Request).我正在尝试调用 POST/PUT(通过使用 HttpClient,在 Swagger 或 Postman 中有效)方法,但它返回 400 状态代码(错误请求)。 It checks for database exist and it is returns okay.它检查数据库是否存在并且返回正常。

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

Also I am using this part of code:我也在使用这部分代码:

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;

I tried to use another API and it works but in my API there is no errors and so on.我尝试使用另一个 API 并且它有效,但在我的 API 中没有错误等等。

As this document said:正如这份文件所说:

Beginning with .NET 6, new projects include the <Nullable>enable</Nullable> element in the project file.从 .NET 6 开始,新项目在项目文件中包含<Nullable>enable</Nullable>元素。 Once the feature is turned on, existing reference variable declarations become non-nullable reference types .一旦启用该功能,现有的引用变量声明就变成了不可为空的引用类型

In .NET 6 the non-nullable property must be required , otherwise the model validation will fail.在 .NET 6 中必须要求不可为空的属性,否则 model 验证将失败。

To achieve your requirement, you can remove <Nullable>enable</Nullable> from your project file.为实现您的要求,您可以从项目文件中删除<Nullable>enable</Nullable>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM