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

Asked

Viewed 86 times

1

I am trying to return a Jsonresult to my View, but it is returning the following error.

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

You could help me friends ?

[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 };

    }

On the Return line I’m getting the error I mentioned above, could help me ?

  • Yeah. Net Core, right? Try it without the JsonRequestBehavior = JsonRequestBehavior.AllowGet, thus return new JsonResult(new { Data = events }). I’m on the phone and Jaja Gero a reply.

1 answer

1


No. Net Core does not have the option JsonRequestBehavior it is managed by the attribute that sits on top of the IActionResult, in your case you already inform that it is a Get with the HttpGet.

There are two options, first return the JsonResult which is the class and in its constructuor awaits an object or an object plus an instance of JsonSerializerSettings or use the method Json who is within the class Controller and 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 class JsonSerializerSettings you can see more on official documentation

Browser other questions tagged

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