我正在嘗試根據 DateTime 引數獲取資料串列。但它不起作用,因為我收到錯誤 AutoMapper.AutoMapperMappingException:缺少型別映射配置或不支持的映射。但是,當我嘗試根據 int ID 引數檢索資料時,它正在作業。關于如何解決這個問題的任何想法?
這是控制器代碼:
[HttpGet("{pubdate}")]
public async Task<ActionResult<ToDoListDto>> GetTodListOfDates(DateTime pubdate)
{
var todo = await _toDoListRepository.GetIncomingToDoAsync(pubdate);
if (todo == null)
{
return NotFound();
}
return Ok(_mapper.Map<ToDoListDto>(todo));
}
[HttpGet("{id}")]
public async Task<ActionResult<ToDoListDto>> GetTodList(int id)
{
var todo = await _toDoListRepository.GetSpecificTodoAsync(id);
if (todo == null)
{
return NotFound();
}
return Ok(_mapper.Map<ToDoListDto>(todo));
}
我的存盤庫代碼:
public async Task<IEnumerable<ToDoList>> GetIncomingToDoAsync(DateTime dateTime)
{
return await _context.ToDoLists.Where(c => c.StartDate == dateTime).ToListAsync();
}
public async Task<ToDoList?> GetSpecificTodoAsync(int taskId)
{
return await _context.ToDoLists.Where(c => c.Id == taskId).FirstOrDefaultAsync();
我的待辦事項模型:
public class ToDoListDto
{
public int Id { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }
public DateTime StartDate { get; set; }
}
uj5u.com熱心網友回復:
您由 datetime 端點開始的映射正在嘗試映射 anIEnumerable<ToDoListDto>而您的整數端點正在映射單個ToDoListDto.
如果要單獨映射集合中的每個專案(這是整數 ID 端點正在執行的操作),請將 datetime 端點中的映射更改為以下內容:
return Ok(todo.Select(item => _mapper.Map<ToDoListDto>(item));
或者,如果要映射整個集合,則必須明確并將型別指定為IEnumerable<ToDoListDto>:
return Ok(_mapper.Map<IEnumerable<ToDoListDto>>(todo));
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466605.html
標籤:C# asp.net-mvc api 约会时间 asp.net-web-api
