我创建了小型 Rest API 应用程序并尝试使用 Postman 对其进行测试,但一无所获。
这是以下代码:
-动物
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; }
}
-动物控制器
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();
}
}
-Program.cs 的内容(由 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();
邮差GET请求:
http://localhost:5039/Animal
我确定我使用的是正确的端口。
如果该信息有帮助: IDE:Rider,模板:ASP.NET Core Web 应用程序模板,类型:Blazor Web App,框架:net8.0
我询问了这个问题,没有给我任何有用的信息。
您似乎创建了错误的应用程序来构建 Web API 应用程序。
您应该使用 ASP.NET Core Web API 而不是 Blazor Web 应用程序。
第1步:选择ASP.NET Core Web API模板
第2步:配置新项目(位置)
步骤3:
在本节中,您将配置要生成的应用程序模板:
框架 - 哪个框架? .NET 6.0、.NET 7.0 等
启用开放 API 支持 - 生成 Swagger API 文档
不要使用顶级语句 - 使用 Main
方法创建
Program.cs,而不使用顶级语句。
使用控制器 - 创建控制器(类)而不是最小的 API。
项目创建完成后,可以看到生成了Program.cs,如下:
using VeterinaryClinicShelter.Components;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers(); // Add services for controllers
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapControllers(); // map attribute routed controllers.
app.Run();