1

I'm creating a very simple aspnet core 2.2 api.

I want trying to return a string of json format as described here ...

ASP.NET Core return JSON with status code

and here ...

https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.2

However the "Json()" helper method is not recognised ... The name Json does not exist in the current context.

This is even though JSonResult is recognised.

The example below from the Microsoft link above. the line "return Json" is what studio doesn't like

// GET: api/authors
[HttpGet]
public JsonResult Get()
{
    return Json(_authorRepository.List());
}

Edit

About the duplication flag:

Note that Microsoft document found in the above link can be open to interpretation. It says "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" . I'm inheriting from "ControllerBase" class, which now I see is different to the "Controller" base class. Also, as this document is related to dot net core api, the default scaffold uses ControllerBase. In my opinion, the document could do with a revision

Cristian Traìna
  • 7,056
  • 1
  • 31
  • 51
  • Even the docs linked in your question notes that you need to inherit from `Controller` to have access to the `Json(...)` helper method. – Nkosi Jun 11 '19 at 12:19
  • It can be open to interpretation. - it says "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" . I'm inheriting from "ControllerBase" class, which now I see is different to the "Controller" base class. Also, as this document is related to dot net core api, the default scaffold uses ControllerBase. In my opinion, the document could do with a revision. – Excelnoobster Jun 11 '19 at 13:15

0 Answers0