简体   繁体   English

Rest Api 将 null 值传递给 ZAC5C74B64B4B8352EF2F18AZ1AFFB5AC2

[英]Rest Api passing null values to sql database

I have an Object which has a property statusDate of type DateTime.我有一个 Object,它的属性 statusDate 的类型为 DateTime。 This property is a nullable type with implementation as:该属性是一个可以为空的类型,实现如下:

public DateTime? statusDate {get;set;}

I am using Rest Api to insert data into the database.我正在使用 Rest Api 将数据插入数据库。 I get error 400 Bad Request.我收到错误 400 错误请求。 After some Googling I have realized that because the value is null my Rest Api url is throwing the error. After some Googling I have realized that because the value is null my Rest Api url is throwing the error. How do I pass null values to Rest Api如何将 null 值传递给 Rest Api

Example: http://localhost:62733/api/Claims/InsertAsync/username=Zainalds&statusDate=null示例:http://localhost:62733/api/Claims/InsertAsync/username=Zainalds&statusDate=null

        public static async Task<long> PostAsyncLong<T>(Uri uri, T postObject)
        {
           var httpContent = new StringContent(JsonSerializer.Serialize(postObject), Encoding.UTF8, "application/json");
           var response = await _httpClient.PostAsync(uri, httpContent, _cancellationToken).ConfigureAwait(false);

           return await GetResult<long>(response);
        }

It depends upon what type of API request you accessing [GET] or [POST]这取决于您访问 [GET] 或 [POST] 的 API 请求的类型

GET REQUEST获取请求

Request accessing you need to handle on code level eg.请求访问您需要在代码级别处理,例如。

  • API URL - api/testController/InsertAsync?username=Zainalds&statusDate=null API URL - api/testController/InsertAsync?username=Zainalds&statusDate=null

     [HttpGet] public string InsertAsync(string username, DateTime? statusDate) { return "value"; }

POST REQUEST发布请求

Request accessing you don't need to assign value for statusDate then default set to null eg.请求访问您不需要为 statusDate 分配值,然后默认设置为 null 例如。

Request Model请求 Model

public class InsertAsyncRequest
{
     public string username { get; set; }
     public DateTime? statusDate { get; set; }
}

In API Controller在 API Controller

 [HttpPost]
 public string InsertAsync(InsertAsyncRequest _req)
 {    
     return "value";
 }

The answer is pretty straight forward make sure all submodules are updated to use latest POCO classes.答案很简单,确保所有子模块都更新为使用最新的 POCO 类。

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

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