0

hi when I run this api/animals it is returning an empty array however it should return data in json format it is .Net 6.0

namespace WebApi.Controllers
{
 [Route("api/[controller]")]
 [ApiController]
 public class AnimalsController : ControllerBase
 {
 public IActionResult GetAnimals(); 
 {
 var animals = new List<AnimalModel> 
 {
 new AnimalModel() { Name = "dog", Description = "4 legs"};
 new AnimalModel() { Name = "cat", Description = "4 legs" };
 };
 return Ok(animals);
 }
 }
}
Tiny Wang
16.7k2 gold badges21 silver badges38 bronze badges
asked Sep 6, 2022 at 9:41
5
  • 3
    Please post code as code, not as images. Commented Sep 6, 2022 at 9:41
  • 2
    Add your code instead of images please. Commented Sep 6, 2022 at 9:46
  • 2
    You should probably read How to ask before posting. Commented Sep 6, 2022 at 9:49
  • You seem to have a thing for semi-colons, try removing some. Commented Sep 6, 2022 at 9:52
  • try my code...... Commented Sep 6, 2022 at 9:52

2 Answers 2

1
  • remove the semicolon in line 13
  • remove the semicolon in line 15, and 16 add (comma),

then try to execute it

answered Sep 6, 2022 at 9:49
1
[Route("api/[controller]")]
[ApiController]
public class AnimalsController : ControllerBase
{
 public IActionResult GetAnimals()
 {
 var animals = new List<AnimalModel> 
 {
 new AnimalModel() { Name = "dog", Description = "4 legs"},
 new AnimalModel() { Name = "cat", Description = "4 legs" }
 };
 return Ok(animals);
 }
}
public class AnimalModel
{
 public string Name { get; set; }
 public string Description { get; set; }
}

enter image description here

answered Sep 6, 2022 at 9:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.