【发布时间】:2020-12-31 11:22:09
【问题描述】:
[HttpPost("add/{userId}")]
public async Task<IActionResult> Add(int userId, CategoryDto categoryDto)
{
if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
return Unauthorized();
categoryDto.UserId = userId;
var category = _mapper.Map<Category>(categoryDto);
_repo.Add(category);
if (await _unitOfWork.Complete())
return Ok("Added successfully");
throw new Exception("Adding Category Falid To Save");
}
这是我的控制器,我不知道该怎么办,为什么会显示这个错误。
using AutoMapper;
using QRmenu.API.Dtos;
using QRmenu.API.Models;
namespace QRmenu.API.Helpers
{
public class AutoMapperProfiles : Profile
{
public AutoMapperProfiles()
{
CreateMap<User, UserForDetailedDto>();
CreateMap<Item, ItemToReturnDto>()
.ForMember(dest => dest.ItemImages, opt =>
{
opt.MapFrom(src => src.ItemImages);
});
CreateMap<Category, CategoryDto>();
}
}
}
这是我的 Automapper 代码。
我需要帮助来解决这个问题。
【问题讨论】:
-
“缺少类型映射配置或不支持的映射。” 对我来说似乎是不言而喻的。考虑到这看起来像是一个映射错误(即当您调用
mapper.Map<TargetType>(sourceObject)时),包含该代码对您很有用。没有minimal reproducible example,我们无能为力。 -
如果不言自明,帮忙
-
嗯,它表示您尝试使用不受支持的映射对。它继续解释您已尝试将
object映射到Category。您没有为此定义映射。您还没有提供minimal reproducible example:您提供了映射配置,但没有提供此错误发生位置的示例。 -
你现在可以显示我的代码吗
-
显然我们需要查看 CategoryController 的 Add 方法中的代码。
标签: c# asp.net-core automapper