2

Though some answers are in this area, none answer my question. This function is a part of a Projects repository. Projects have a lot of items, but there are times when only the "Header" elements are needed from it. The Header is NOT stored separately, but has a different "API..Dto". (This has to get shipped back to the front-end)

public async Task<ActionResult<List<ProjectAPIHeaderDto>>> GetAllProjectHeaders()
{
    var projects = await _context.Projects.ToListAsync();  // full project records
    var records = _mapper.Map<List<ProjectAPIHeaderDto>>(projects);  // only some fields from project
    return records;
}

The problem is:

Cannot implicitly convert type 'System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.List<API.DTOs.ProjectAPIHeaderDto>>>' to 'System.Collections.Generic.List<API.DTOs.ProjectAPIHeaderDto>'

Similar code works perfectly in another function shown here:

public async Task<List<ProjectAPIDto>> GetAllProjects()
{
    var projects = await _context.Projects.ToListAsync();
    var records = _mapper.Map<List<ProjectAPIDto>>(projects);
    return records;
}

ProjectAPIDto has all fields from the projects while ProjectAPIHeaderDto has a few of the project fields.

This link is hover over var records and shows it is List. enter image description here

this is ProjectAPIHeaderDto:

using System.ComponentModel.DataAnnotations;

namespace API.DTOs
{
    public class ProjectAPIHeaderDto
    {
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        [Required]
        public string Description { get; set; }

    }
}

Looking again at the error message: System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.ActionResult I've never seen this problem before anywhere.. is ActionResult the problem?

Thanks in advance for your help.

  • 1
    `ActionResult>` is not the same as `List`. – Poul Bak Nov 20 '22 at 18:51
  • Yes, I see that; however ActionResult does not require that I add anything like Ok(dto) wrapper; in fact, I tried that and it didn't make any difference. I need "Task" so I can async call to get all projects. Anything more you can add? :-) – Chuck Duncan Nov 20 '22 at 19:29
  • do they share all the same fields, or does ProjectAPIDto actually extend ProjectAPIHeaderDto? In C#, doesn't matter if they have same properties, still different types – Garr Godfrey Nov 20 '22 at 19:32
  • API dto has LOTS of fields, but APIHeader only has a few of those same fields. The error is in the first small function shown at the top... it is asking for List-of-APIHeader to be returned and mapper is creating that. Point being neither is defined as an extension of the other .. Yes, they are different types. (Thanks for checking this out.) – Chuck Duncan Nov 20 '22 at 19:42

1 Answers1

0

Try to use Task<IActionResult> instead and wrap your result in an Ok(...) (or whatever is needed in your response).

public async Task<IActionResult> GetAllProjectHeaders()
{
    var projects = await _context.Projects.ToListAsync();  // full project records
    var records = _mapper.Map<List<ProjectAPIHeaderDto>>(projects);  // only some fields from project
    return Ok(records);
}

Here you can find a good comparison of the three options.

Note: If your are using any kind of client generation tool (Nswag, Swashbuckle, ...) you need to specify a response type attribute because the IActionResult has no T

Martin
  • 3,096
  • 1
  • 26
  • 46