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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

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.