在ASP.Net Core 2.2中,复杂对象以NULL的形式出现在HttpPut中 [英] Complex object is coming as NULL for HttpPut in ASP.Net Core 2.2

查看:35
本文介绍了在ASP.Net Core 2.2中,复杂对象以NULL的形式出现在HttpPut中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Asp.Net Core 2.2构建一个webapi.我的一个控制器有一个Put方法来更新实体.问题是,我从邮递员休息客户端传递的复杂对象始终为空.

I am building a webapi using Asp.Net Core 2.2. One of my controllers has a Put method to update an entity. The problem is, the complex object I pass from postman rest client is always 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类:

SearchPattern class:

[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; }
}

邮递员请求:

这是我到目前为止尝试过的:

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)

调试输出:

推荐答案

searchPattern 参数设置为 null ,因为模型绑定器至少获得了一个无效的模型属性值.在您的情况下,很明显 updateDate 被映射为 DateTime 类型,但是指定的值具有无效的格式

The searchPattern parameter is set to null because model binder got at least one invalid value for model property. In your case it's obvious that updateDate is mapped to DateTime type but specified value has invalid format

2019-03-27T01:20:00 PM

PM 部分在这里无效,只需将其删除

PM part is invalid here, just remove it

2019-03-27T01:20:00

还要确保所有其他字段都具有有效的值来解决您的问题.

Also make sure all other fields have valid values to fix your problem.

这篇关于在ASP.Net Core 2.2中,复杂对象以NULL的形式出现在HttpPut中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆