简体   繁体   English

在ASP.Net Core 2.2中,复杂对象以NULL的形式出现在HttpPut中

[英]Complex object is coming as NULL for HttpPut in ASP.Net Core 2.2

I am building a webapi using Asp.Net Core 2.2. 我正在使用Asp.Net Core 2.2构建一个webapi。 One of my controllers has a Put method to update an entity. 我的一个控制器有一个Put方法来更新实体。 The problem is, the complex object I pass from postman rest client is always null. 问题是,我从邮递员休息客户端传递的复杂对象始终为null。

[HttpPut]
[Route("{searchPatternId:long}")]
public async Task<IActionResult> Put(long searchPatternId, [FromBody]SearchPattern searchPattern)
{
        try
        {
            if (searchPattern == null) return BadRequest();

            return Ok(await _searchPatternService.PutAsync(searchPattern));
        }
        catch (Exception e)
        {
            await _errorLogService.Log(e);
            return StatusCode(500);
        }
}

SearchPattern class: SearchPattern类:

[Table("search_pattern", Schema = "abc")]
public class SearchPattern
{
    [Key]
    public long SearchPatternId { get; set; }
    public string Pattern { get; set; }
    public long PatternHash { get; set; }
    public int? Age { get; set; }
    public string Location { get; set; }
    public long TotalRecordsFound { get; set; }
    public int TotalPages { get; set; }
    public int LastSearchedPage { get; set; }
    public string MachineName { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public string UpdatedBy { get; set; }
    public DateTime? UpdatedDate { get; set; }
    public string Url { get; set; }
}

Postman request: 邮递员要求:

在此处输入图片说明

This is what I have tried so far: 到目前为止,这是我尝试过的:

[HttpPut]
[Route("{searchPatternId:long}")]
public async Task<IActionResult> Put([FromRoute]long searchPatternId, [FromBody]SearchPattern searchPattern)

[HttpPut("{searchPatternId:long}")]    
public async Task<IActionResult> Put([FromRoute]long searchPatternId, [FromBody]SearchPattern searchPattern)

[HttpPut]
public async Task<IActionResult> Put([FromBody]SearchPattern searchPattern)

Debugging Output: 调试输出:

在此处输入图片说明

The searchPattern parameter is set to null because model binder got at least one invalid value for model property. searchPattern参数设置为null因为模型绑定器为模型属性获得了至少一个无效值。 In your case it's obvious that updateDate is mapped to DateTime type but specified value has invalid format 在您的情况下,很显然updateDate映射到DateTime类型,但是指定的值具有无效的格式

2019-03-27T01:20:00 PM

PM part is invalid here, just remove it PM部分在这里无效,只需将其删除

2019-03-27T01:20:00

Also make sure all other fields have valid values to fix your problem. 还要确保所有其他字段都具有有效的值来解决您的问题。

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

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