


























Install-Package Swashbuckle.AspNetCore
namespace WebApplication1.Models { public class Book { public int Id { get; set; } public string Name { get; set; } public string ISBN { get; set; } public string Author { get; set; } public string Abstract { get; set; } public string Comment { get; set; } public string Content { get; set; } public string Summary { get; set; } public string Title { get; set; } public string Topic { get; set; } } } using WebApplication1.Models; namespace WebApplication1.Services { public class BookService { static int Idx = 0; private int GetIdx() { return Interlocked.Increment(ref Idx); } public List<Book> GetBooksList() { List<Book> booksList=new List<Book>(); for(int i=0;i<1000;i++) { var idx=GetIdx(); var bk = new Book() { Id=idx, Name=$"Name_{idx}", ISBN=$"ISBN_{idx}_{Guid.NewGuid():N}", Author=$"Author_{idx}", Abstract=$"Abstract_{idx}", Comment=$"Comment_{idx}", Content=$"Content_{idx}", Summary=$"Summary_{idx}", Title=$"Title_{idx}", Topic=$"Topic_{idx}" }; booksList.Add(bk); } return booksList; } } } using Microsoft.AspNetCore.Mvc; using WebApplication1.Models; using WebApplication1.Services; namespace WebApplication1.Controllers { [ApiController] [Route("api/[controller]")] public class BookController : Controller { private readonly BookService bkService; public BookController(BookService bkServiceValue) { bkService = bkServiceValue; } [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] public ActionResult<List<Book>> GetBooks() { var books = bkService.GetBooksList(); return Ok(books); } [HttpGet("{id}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public ActionResult<Book> GetBookById(int id) { var books = bkService.GetBooksList(); var bk=books.FirstOrDefault(x => x.Id == id); if(bk==null) { return NotFound($"Can't find book whose ID is {id}"); } return Ok(bk); } } } using WebApplication1.Services; namespace WebApplication1 { public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); builder.Services.AddSingleton<BookService>(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); } } }

https://localhost:7205/api/book/9250 { "id": 9250, "name": "Name_9250", "isbn": "ISBN_9250_3d940005204f46abb2bd3ced7e17b4a8", "author": "Author_9250", "abstract": "Abstract_9250", "comment": "Comment_9250", "content": "Content_9250", "summary": "Summary_9250", "title": "Title_9250", "topic": "Topic_9250" }
https://localhost:7205/api/book


此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。