Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonResult return Json in ASP.NET CORE 2.1

Controller that worked in ASP.NET Core 2.0:

[Produces("application/json")] [Route("api/[controller]")] [ApiController] public class GraficResourcesApiController : ControllerBase {         private readonly ApplicationDbContext _context;      public GraficResourcesApiController(ApplicationDbContext context)     {         _context = context;     }      [HttpGet]     public JsonResult GetGrafic(int ResourceId)     {         var sheduling = new List<Sheduling>();           var events = from e in _context.Grafic.Where(c=>c.ResourceId == ResourceId)                      select new                      {                          id = e.Id,                          title = e.Personals.Name,                          start = e.DateStart,                          end = e.DateStop,                          color = e.Personals.Color,                          personalId = e.PersonalId,                          description = e.ClientName                      };         var rows = events.ToArray();          return Json(rows);     } } 

in ASP.NET Core 2.1

return Json (rows); 

writes that Json does not exist in the current context. If we remove Json leaving simply

return rows; 

then writes that it was not possible to explicitly convert the type List () to JsonResult

How to convert to Json now?

like image 438
blakcat Avatar asked Aug 30 '18 15:08

blakcat


People also ask

How do I return JSON in Web API net core?

To return data in a specific format from a controller that inherits from the Controller base class, use the built-in helper method Json to return JSON and Content for plain text. Your action method should return either the specific result type (for instance, JsonResult ) or IActionResult .

How do I return a JSON response in C#?

ContentType = "application/json; charset=utf-8"; Response. Write(json); Response. End(); To convert a C# object to JSON you can use a library such as Json.NET.

What is JsonResult .NET core?

An action result which formats the given object as JSON.

What does JsonResult return?

JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format).


1 Answers

In asp.net-core-2.1 ControllerBase does not have a Json(Object) method. However Controller does.

So either refactor the current controller to be derived from Controller

public class GraficResourcesApiController : Controller {     //... } 

to have access to the Controller.Json Method or you can initialize a new JsonResult yourself in the action

return new JsonResult(rows); 

which is basically what the method does internally in Controller

/// <summary> /// Creates a <see cref="JsonResult"/> object that serializes the specified <paramref name="data"/> object /// to JSON. /// </summary> /// <param name="data">The object to serialize.</param> /// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/> /// to JSON format for the response.</returns> [NonAction] public virtual JsonResult Json(object data) {     return new JsonResult(data); }  /// <summary> /// Creates a <see cref="JsonResult"/> object that serializes the specified <paramref name="data"/> object /// to JSON. /// </summary> /// <param name="data">The object to serialize.</param> /// <param name="serializerSettings">The <see cref="JsonSerializerSettings"/> to be used by /// the formatter.</param> /// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/> /// as JSON format for the response.</returns> /// <remarks>Callers should cache an instance of <see cref="JsonSerializerSettings"/> to avoid /// recreating cached data with each call.</remarks> [NonAction] public virtual JsonResult Json(object data, JsonSerializerSettings serializerSettings) {     if (serializerSettings == null)     {         throw new ArgumentNullException(nameof(serializerSettings));     }      return new JsonResult(data, serializerSettings); } 

Source

like image 161
Nkosi Avatar answered Sep 27 '22 17:09

Nkosi