4

I'm trying to handle returning data to APi client in GET HTTP response in asynchronous manner but with no luck so far.

My code :

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Server.Database;
using System;
using System.Threading.Tasks;

namespace Server.Controllers
{
    //[Produces("application/json")]
    [Route("api/[Controller]")]
    public class UserController : Controller
    {
        private readonly DBContext _context;

        public UserController(DBContext context)
        {
            _context = context;
        }

        [HttpGet("/Users")]
        public async Task<IAsyncResult> GetUsers()
        {
            using (_context)
            {
                // how to properly return data asynchronously ? 
                var col = await _context.Users.ToListAsync();
            }
        }

        [HttpGet("/Users/{id}")]
        public async Task<IActionResult> GetUserByID(Int32 id)
        {

            using (_context)
            {
                //this is wrong, I don't knwo how to do it properly
                //var item = await new ObjectResult(_context.Users.FirstOrDefault(user => user.IDUser == id));
            }
        }
    }
}

As you can see I would like to handle GET request asynchronously by returning all users and in another method single user by his ID. I don't know if I need ObjectResultclass as well but I need to respons with JSON object to the client. Someone know how to do this ?

3
  • Firstly, using DbContext is a bad idea, its better to register it in Startup, secondly, try to return Json, something like this: return Json(await _context.Users.ToListAsync()); Commented Jul 7, 2017 at 10:02
  • Thanks for the answer @Yuriy N. The problem with Json(await _context.Users.ToListAsync()) is that I have Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.JsonResult' to 'System.IAsyncResult' error. Commented Jul 7, 2017 at 10:10
  • I've answered about that problem. Commented Jul 7, 2017 at 10:12

1 Answer 1

3

Here, try this:

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Server.Database;
using System;
using System.Threading.Tasks;

namespace Server.Controllers
{
    //[Produces("application/json")]
    [Route("api/[Controller]")]
    public class UserController : Controller
    {
        private readonly DBContext _context;

        public UserController(DBContext context)
        {
            _context = context;
        }

        [HttpGet("/Users")]
        public async Task<IActionResult> GetUsers()
        {
            return Json(await _context.Users.ToListAsync());            
        }

        [HttpGet("/Users/{id}")]
        public async Task<IActionResult> GetUserByID(Int32 id)
        {
            return Json(await new _context.Users.FirstOrDefault(user => user.IDUser == id));          
        }
    }
}

Note, that in GetUsers you must return IActionResult, not IAsyncResult.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.