I've created small Rest API app and tried to test it using Postman, but got nothing.
That's pieces of code for:
-Animal
namespace VeterinaryClinicShelter.Models;public class Animal{ public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public double Weight { get; set; } public string FurColor { get; set; }}
-AnimalController
using Microsoft.AspNetCore.Mvc;using VeterinaryClinicShelter.Models;namespace VeterinaryClinicShelter.Controllers;[ApiController][Route("[controller]")]public class AnimalController : ControllerBase{ private static readonly List<Animal> Animals = [ new Animal { Id = 1, Name = "Buddy", Category = "Dog", Weight = 15.2, FurColor = "Brown" }, new Animal { Id = 2, Name = "Whiskers", Category = "Cat", Weight = 7.5, FurColor = "White" }, new Animal { Id = 3, Name = "Fluffy", Category = "Cat", Weight = 6.8, FurColor = "Gray" }, new Animal { Id = 4, Name = "Max", Category = "Dog", Weight = 20.5, FurColor = "Black" }, new Animal { Id = 5, Name = "Snowball", Category = "Rabbit", Weight = 3.2, FurColor = "White" } ]; [HttpGet] public ActionResult<List<Animal>> GetAnimals() { return Animals; } [HttpGet("{id}")] public ActionResult<Animal> GetAnimalById(int id) { var animal = Animals.FirstOrDefault(a => a.Id == id); if (animal == null) { return NotFound(); } return animal; } [HttpPost] public ActionResult<Animal> AddAnimal(Animal animal) { if (Animals.Any(a => a.Id == animal.Id)) { return Conflict("Animal with such ID already exists"); } Animals.Add(animal); return CreatedAtAction(nameof(GetAnimalById), new { id = animal.Id }, animal); } [HttpPut("{id:int}")] public ActionResult<Animal> EditAnimal(int id, Animal newAnimal) { var index = Animals.FindIndex(animal => animal.Id == id); if (index == -1) return NotFound(); Animals[index].Name = newAnimal.Name; Animals[index].Category = newAnimal.Category; Animals[index].Weight = newAnimal.Weight; Animals[index].FurColor = newAnimal.FurColor; return NoContent(); } [HttpDelete("{id:int}")] public ActionResult<Animal> DeleteAnimal(int id) { var deleteAnimal = Animals.FirstOrDefault(animal => animal.Id == id); if (deleteAnimal == null) return NotFound(); Animals.Remove(deleteAnimal); return NoContent(); }}
-Content of Program.cs (auto generated by Rider)
using VeterinaryClinicShelter.Components;var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddRazorComponents() .AddInteractiveServerComponents();var app = builder.Build();// Configure the HTTP request pipeline.if (!app.Environment.IsDevelopment()){ app.UseExceptionHandler("/Error", createScopeForErrors: true); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseAntiforgery();app.MapRazorComponents<App>() .AddInteractiveServerRenderMode();app.Run();
Postman GET request:
http://localhost:5039/Animal
I'm sure that I'm using the right port.
In case that info will be helpfull:IDE: Rider, Template: ASP.NET Core Web Application template, Type: Blazor Web App, Framework: net8.0
I asekd chat about that problem, gave me no useful info.