确认 AutoMapper 是否已经被正确地设置及配置,确保 MapperConfiguration 中包括了相应的命名空间,如下所示:
// Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); }
使用 CreateMap 方法创建类型映射配置,确保源类型和目标类型都被正确地匹配。例如:
// AutoMapperProfile.cs
public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
// Source: Book, Destination: BookDTO
CreateMap
确保配置文件中使用了正确的映射名称。例如:
// appsettings.json "Automapper": { "Profiles": [ { "ProfileName": "MyProfile", "MappingAssembly": "MyApplication.Assembly.Name" } ] }
如果出现了缺少类型映射暗示错误,可以手动调用 CreateMap 方法以创建缺少的类型映射。
// In your controller or service:
var configuration = new MapperConfiguration(cfg =>
{
cfg.CreateMap
参考链接:https://stackoverflow.com/questions/59596577/asp-net-core-web-api-automapper-getting-error-mapping-types-missing-type-ma
上一篇:Asp.NetCore3.1WebApi尝试更新记录时会将其删除。
下一篇:ASP.NETCore3.1WebApiHttpPostAction参数无法接收axios的application/jsonHttpPost传递的数据。