From f15aa02600fdcd03c45fc5e0bad1606518035c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Velimir=20=C4=90urkovi=C4=87?= Date: Fri, 20 Dec 2024 00:00:16 +0100 Subject: [PATCH] Move from controller to minimal API --- .../Controllers/PasswordController.cs | 24 ------------------- DemoDotnetWebApi/Program.cs | 20 +++++++++------- 2 files changed, 11 insertions(+), 33 deletions(-) delete mode 100644 DemoDotnetWebApi/Controllers/PasswordController.cs diff --git a/DemoDotnetWebApi/Controllers/PasswordController.cs b/DemoDotnetWebApi/Controllers/PasswordController.cs deleted file mode 100644 index 896c215..0000000 --- a/DemoDotnetWebApi/Controllers/PasswordController.cs +++ /dev/null @@ -1,24 +0,0 @@ -using DemoDotnetWebApi.Interfaces; -using Microsoft.AspNetCore.Mvc; - -namespace DemoDotnetWebApi.Controllers; - -[ApiController] -[Route("api/v1/[controller]")] -public class PasswordController : ControllerBase -{ - private readonly ILogger _logger; - private readonly IPasswordGenerator _passwordGenerator; - - public PasswordController(ILogger logger, IPasswordGenerator passwordGenerator) - { - _logger = logger; - _passwordGenerator = passwordGenerator; - } - - [HttpGet] - public string Get() - { - return _passwordGenerator.Generate(); - } -} diff --git a/DemoDotnetWebApi/Program.cs b/DemoDotnetWebApi/Program.cs index 1b8cd58..ce130b5 100644 --- a/DemoDotnetWebApi/Program.cs +++ b/DemoDotnetWebApi/Program.cs @@ -8,24 +8,26 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddOpenApi(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); + app.MapOpenApi(); + app.UseSwaggerUI(options => + { + options.SwaggerEndpoint("/openapi/v1.json", "Demo API"); + }); } app.UseHttpsRedirection(); -app.UseAuthorization(); - -app.MapControllers(); +app.MapGet("/api/v1/password", (IPasswordGenerator _passwordGenerator) => +{ + return _passwordGenerator.Generate(); +}) +.WithName("GetPassword"); app.Run();