Automapper Error Rest API缺少类型映射配置或不受支持的映射

时间:2020-09-23 21:21:07

标签: asp.net asp.net-web-api

我正在用我的Automapper解决这个错误。我不知道应该在哪里解决此问题。这是错误的详细信息

AutoMapper.AutoMapperMappingException HResult = 0x80131500
Message =缺少类型映射配置或不支持的映射。
来源= StackTrace:

这是startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using ipet_core_api.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ipet_core_api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<TaskUatContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("TasksUatConnection")));

            services.AddControllers();

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

            services.AddScoped<ITaskUatRepo, TaskUatRepo>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

这是我的Automapper个人资料

using AutoMapper;
using ipet_core_api.Dtos;
using ipet_core_api.Model;

namespace ipet_core_api.Profiles
{
    public class TaskProfile : Profile
    {
        public TaskProfile()
        {
            //Source -> Target
            CreateMap<TaskModel, TaskReadDto>();
        }
    }
}

这是我的Dto taskReadDto

using System;

namespace ipet_core_api.Dtos
{
    public class TaskReadDto
    {
        
        public int Id { get; set; }
        
        public string Title { get; set; }
        public int PrimaryAssignedTo { get; set; }
        public int SecondaryAssignedTo { get; set; }
        
        public int LinkedID { get; set; }
        public DateTime DueDate { get; set; }
        
        public int TaskPriority { get; set; }
        
        public int CreatedBy { get; set; }
        
        public DateTime Created { get; set; }
        
        public int ModifiedBy { get; set; }
        public DateTime Modifed { get; set; }
        
        public Boolean TaskCompleted { get; set; }
        
        public Boolean Deleted { get; set; }
    }
}

在我的控制器上,我有这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using ipet_core_api.Data;
using ipet_core_api.Dtos;
using ipet_core_api.Model;
using Microsoft.AspNetCore.Mvc;

namespace ipet_core_api.Controllers
{
    [Route("api/uat/tasks/[action]")]
    [ApiController]
    public class TaskUatController : ControllerBase
    {
        private readonly ITaskUatRepo _repository;
        private readonly IMapper _mapper;

        public TaskUatController(ITaskUatRepo repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }
 
       
        //api/uat/tasks/GetTasks?LinkedID=1
        [HttpGet]
        public ActionResult<IEnumerable<TaskReadDto>> GetTasks([FromQuery] int LinkedID)
        {
            var tasksItem = _repository.GetTasks(LinkedID);


            return Ok(_mapper.Map<TaskReadDto>(tasksItem));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找出了问题

我只是意识到我需要将IEnumerable添加到映射器

来自:

return Ok(_mapper.Map<TaskReadDto>(tasksItem));

收件人:

 return Ok(_mapper.Map<IEnumerable<TaskReadDto> > (tasksItem));