我正在尝试在 ASP.NET Core 中执行搜索功能。这是我的 Controller :
[HttpPost("searchbyname")]
public async Task<ActionResult<List<SelectedChildDto>>> SearchByName([FromBody]string firstName)
{ if (string.IsNullOrWhiteSpace(firstName)) { return new List<SelectedChildDto>(); }
return await _context.Children
.Where(x => x.FirstName.Contains(firstName))
.OrderBy(x => x.FirstName)
.Select(x => new SelectedChildDto { Cld = x.Cld, ChildCode = x.ChildCode,
FirstName = x.FirstName, PhotoPath = x.PhotoPath })
.Take(5)
.ToListAsync();
}
我从模型中有这个 DTO 类
namespace API.Dtos
{
public class SelectedChildDto
{
public int Cld { get; set; }
public string ChildCode { get; set; }
public string FirstName { get; set; }
public string PhotoPath { get; set; }
}
}
这是我的错误输出:
"errors": {
"$": [
"The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
]
}
我该如何解决这个问题?
最佳答案
该问题与 Controller 的单个 [FromBody] string firstName
参数的正文文本的 System.Text.Json 反序列化有关。例如,您只需在正文中发送 "John"
,而不是完整功能的 JSON {"firstName": "John"}
。
另请检查this answer类似的问题。
关于ASP.NET Core 错误 : The JSON value could not be converted to System. 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72385282/