"JsonResult" does not contain a constructor that accepts 0 arguments

1

I'm trying to return a JsonResult for my View, but it's returning the following error.

  

"JsonResult" does not contain a constructor that accepts 0 arguments

Would you help me out?

[HttpGet]
public async Task<IActionResult> GetAllSchedule()
{
    var user = await _userManager.GetUserAsync(User);

    if (user == null)
    {
        throw new ApplicationException($"Não é possível carregar o usuário com o ID '{_userManager.GetUserId(User)}'.");
    }

    var events = _scheduleManager.GetAllSchedule(user.Id);

    return new JsonResult { Data = events, JsonRequestBehavior = JsonRequestBehavior.AllowGet };

    }

In the return line I'm getting the error I mentioned above, could you help me?

    
asked by anonymous 16.10.2018 / 19:07

1 answer

1

In .Net Core it does not have the option JsonRequestBehavior it is managed by the attribute that is above IActionResult , in your case you already inform that it is a Get with HttpGet .

There are two options, first to return the JsonResult which is the class and in your constructor expects an object or an object plus an instance of JsonSerializerSettings or use the method Json that is inside the class Controller e will return a JsonResult and it also expects an object or an object plus an instance of JsonSerializerSettings

Your IActionResult would look like this:

[HttpGet]
public async Task<IActionResult> GetAllSchedule()
{
    var user = await _userManager.GetUserAsync(User);

    if (user == null)
    {
        throw new ApplicationException($"Não é possível carregar o usuário com o ID '{_userManager.GetUserId(User)}'.");
    }

    var events = _scheduleManager.GetAllSchedule(user.Id);

    return new JsonResult(new { Data = events });
}

Or

[HttpGet]
public async Task<IActionResult> GetAllSchedule()
{
    var user = await _userManager.GetUserAsync(User);

    if (user == null)
    {
        throw new ApplicationException($"Não é possível carregar o usuário com o ID '{_userManager.GetUserId(User)}'.");
    }

    var events = _scheduleManager.GetAllSchedule(user.Id);

    return Json(new { Data = events });
}

As for the JsonSerializerSettings class you can read more about official documentation

    
16.10.2018 / 20:07