
一步步教你進行 Python REST API 身份驗證
項目結構:
/Interfaces
IPokemonRepository.cs
/Repositories
PokemonRepository.cs
/DTOs
PokemonDto.cs
/Helpers
MappingProfiles.cs
/Controllers
PokemonController.cs
/Data
AppDbContext.cs
Program.cs
中添加 DbContext、AutoMapper 及倉儲依賴注入。在 Interfaces/IPokemonRepository.cs
中,定義 GET 相關方法:
public interface IPokemonRepository
{
IReadOnlyCollection <Pokemon > GetPokemons(); // 列表查詢
Pokemon GetPokemon(int id); // 詳情查詢
Pokemon GetPokemon(string name); // 按名稱查詢
decimal GetPokemonRating(int id); // 評分統計
bool PokemonExists(int id); // 存在性檢測
}
返回所有寶可夢,適用于首頁或 Feed 場景。
根據 ID 返回單個寶可夢詳情,對應產品詳情頁。
支持按名稱搜索,適合前端實現搜索或模糊匹配功能。
根據評論表計算平均評分,返回 decimal
。
簡單布爾檢測,常用于提前判斷資源是否存在。
在 Repositories/PokemonRepository.cs
中實現接口方法:
public class PokemonRepository : IPokemonRepository
{
private readonly AppDbContext _context;
public PokemonRepository(AppDbContext context) = > _context = context;
public IReadOnlyCollection < Pokemon > GetPokemons() = >
_context.Pokemons.OrderBy(p = > p.Id).ToList();
public Pokemon GetPokemon(int id) = >
_context.Pokemons.FirstOrDefault(p = > p.Id == id);
public Pokemon GetPokemon(string name) = >
_context.Pokemons.FirstOrDefault(p = > p.Name == name);
public decimal GetPokemonRating(int id)
{
var reviews = _context.Reviews.Where(r = > r.Pokemon.Id == id).ToList();
if (reviews.Count == 0) return 0m;
return reviews.Sum(r = > r.Rating) / (decimal)reviews.Count;
}
public bool PokemonExists(int id) = >
_context.Pokemons.Any(p = > p.Id == id);
}
public class PokemonDto { … }
public class MappingProfiles : Profile { … }
在 Controllers/PokemonController.cs
中,注入倉儲與映射器,并實現各個 GET Endpoint。
public class PokemonController : ControllerBase
{
private readonly IPokemonRepository _repo;
private readonly IMapper _mapper;
…
}
// GET api/pokemon
[HttpGet]
public ActionResult < IReadOnlyCollection < PokemonDto > > GetPokemons()
{
var pokemons = _repo.GetPokemons();
var dtos = _mapper.Map < IReadOnlyCollection < PokemonDto > > (pokemons);
return Ok(dtos);
}
// GET api/pokemon/{id}
[HttpGet("{id}")]
public ActionResult < PokemonDto > GetPokemon(int id)
{
if (!_repo.PokemonExists(id))
return NotFound($"Pokemon {id} 未找到。");
var pokemon = _repo.GetPokemon(id);
var dto = _mapper.Map < PokemonDto > (pokemon);
return Ok(dto);
}
// GET api/pokemon/name/{name}
[HttpGet("name/{name}")]
public ActionResult < PokemonDto > GetPokemonByName(string name)
{
var pokemon = _repo.GetPokemon(name);
if (pokemon == null) return NotFound($"Name={name} 的 Pokemon 未找到。");
return Ok(_mapper.Map < PokemonDto > (pokemon));
}
// GET api/pokemon/exists/{id}
[HttpGet("exists/{id}")]
public ActionResult < bool > PokemonExists(int id)
{
return Ok(_repo.PokemonExists(id));
}
// GET api/pokemon/rating/{id}
[HttpGet("rating/{id}")]
public ActionResult < decimal > GetPokemonRating(int id)
{
if (!_repo.PokemonExists(id))
return NotFound($"Pokemon {id} 未找到。");
var rating = _repo.GetPokemonRating(id);
return Ok(rating);
}
GET /api/pokemon
,應返回所有寶可夢 DTO 列表。GET /api/pokemon/1
,若存在返回 200 OK + DTO
,否則 404 Not Found
。GET /api/pokemon/name/Pikachu
,檢查大小寫敏感性。GET /api/pokemon/exists/1
,返回布爾值。GET /api/pokemon/rating/1
,返回 decimal
格式平均分,若無評論返回 0.0
。Interfaces
層預先定義所有 GET 方法,提升可維護性。通過以上步驟,你已掌握 ASP.NET Core Web API 中最全面的 GET 接口設計與實現方法。祝你快速構建高質量、易維護的后端服務!