The Controller API is automatically generated by convention
-
Create ASP.NET Core WebApi(Or MVC) Project
dotnet new webapi -o SampleWebAPI
-
Install components via .Net CLI
dotnet add package EasyAPICore --version 1.0.3
-
Add new SampleService
using EasyAPICore; using Microsoft.AspNetCore.Mvc; using System; namespace SampleWebAPI { public class SampleService : IEasyAPI { public BookReadDto Create(BookDto book) { return new BookReadDto { Id = new Random().Next(1, 100), Name = book.Name, Description = book.Description }; } public string Get(int id) { var dto = new BookReadDto { Id = id, Name = "Three body", Description = "The Trisolaran Dark Forest" }; return dto.ToString(); } public string Delete(int id) { var dto = new BookReadDto { Id = id, Name = "Three body", Description = "The Trisolaran Dark Forest" }; return $"Delete {dto.ToString()} Success!" ; } } public class BookDto { /// <summary> /// Book Name /// </summary> public string Name { get; set; } /// <summary> /// Book Description /// </summary> public string Description { get; set; } } public class BookReadDto { /// <summary> /// Book Id /// </summary> public int Id { get; set; } /// <summary> /// Book Name /// </summary> public string Name { get; set; } /// <summary> /// Book Description /// </summary> public string Description { get; set; } public override string ToString() { return $"Book ID:{Id},Book Name:{Name},Book Description:{Description}"; } } }
-
Registered EasyAPICore
public void ConfigureServices(IServiceCollection services) { //Register Swagger services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "SampleWebAPI", Version = "v1" }); //Must Add this c.DocInclusionPredicate((docName, description) => true); }); services.AddEasyAPICore(); }
-
Add Middleware EasyAPICore
app.UseEasyAPICore();
-
Run Sample
dotnet run
Thanks for following project.