I’m not getting a list of an API

Asked

Viewed 107 times

2

This URL works on Postman: http://localhost:56137/api/Getcidade. I need now in another MVC project to bring the list of cities. When I do this I catch this mistake:

The template item inserted in the dictionary is from type'System.Threading.Tasks.Task1[System.Collections.Generic.List1[Trainingcrud.Models.City]', but this dictionary requires an item like 'System.Collections.Generic.Ienumerable`1[Trainingcrud.Services.Getcitiesasync]'.

In the debug I arrive at the controller

public ActionResult Index()
{
    GetCidadesAsync cidadeAsync = new GetCidadesAsync();
    var model = cidadeAsync.GetCidades();
    return View(model);
}

And also from that controller, I arrive in Getcidades()

public class GetCidadesAsync
    {
        HttpClient client = new HttpClient(); 
        public async Task<List<Cidade>> GetCidades()
        {
            string url = $"http://localhost:56137/api/GetCidade";
            var response = await client.GetStringAsync(url);
            var _cidade = JsonConvert.DeserializeObject<List<Cidade>>(response);

            return _cidade.ToList();
        }
    }

When you get on that line: var response = await client.GetStringAsync(url);, he opens the method in my project which is in another project, another Solution. This is the method which is in another Solution

[RoutePrefix("api/[controller]")]
    public class GetCidadeController : ApiController
    {
        GetCidade getCidades = new GetCidade();

        [HttpGet]
        public IEnumerable<Cidade> GetCidadesAsync()
        {
            return getCidades.GetCidades().AsEnumerable().ToList();
        }

    }

This is the controller that takes the list of cities from the bank. The method in my API to take the list from the bank

public class GetCidade
    {
        BancoContext banco = new BancoContext();  
        public List<Cidade> GetCidades()
        {
            return banco.Database.SqlQuery<Cidade>("sp_cons_cidade").ToList();
        }
    }

My Model

public class Cidade
{
  [Key]
  public int id { get; set; }
  [Required]         
  public String nome { get; set; }
}

Why am I making this mistake?

EDIT1

I changed the cshtml model and now this error has come

The template item inserted in the dictionary is from type'System.Threading.Tasks.Task1[System.Collections.Generic.List1[Trainingcrud.Models.City]', but this dictionary requires an item like 'System.Collections.Generic.Ienumerable`1[Trainingcrud.Models.City]'.

Pretty much the same thing, except they’re pointing to the same place Trainingcrud.Models.City

1 answer

2


In your controller’s Index, change the return of the method to a Task<ActionResult> and use the await to await the return of the method GetCidades, because this method returns a Task and not a collection of Cidade (as the error message says):

public async Task<ActionResult> Index()
{
    GetCidadesAsync cidadeAsync = new GetCidadesAsync();
    var model = await cidadeAsync.GetCidades();
    return View(model);
}

It is important to always be aware when returning a Task, because somehow, you must return this task and wait for the end of it to have access to the result object. And that’s what the await ago

Browser other questions tagged

You are not signed in. Login or sign up in order to post.