I have created an ASP.NET Core project in web API, in my controller I am trying to list my users but it gives me an error, the list of users is in my controller I have only added two users, what am I failing in? is it ok to create my user list in my controller?
this is my controller
using Microsoft.AspNetCore.Mvc;
using Project001.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Project001.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
// GET: api/<UserController>
[HttpGet("lista")]
public ActionResult listaUsuarios(int id)
{
var usuarios = from user in users()
where user.id.Equals(id)
select user;
return (ActionResult)usuarios;
}
[NonAction]
public List<User> users()
{
return new List<User>
{
new User
{
id=1,
nombre="juan",
apellido="torres",
numero=12345
},
new User
{
id=2,
nombre="pepe",
apellido="luna",
numero=678910
}
};
}
}
}
this is the error i get
You just have to wrap the result in a method that returns an ObjectResult, As is the Method
Ok()
, you would have something like this: