From ab7b3f6a4e9913da3f94c685dcbede7f1d04d2cc Mon Sep 17 00:00:00 2001 From: Angelo Pirola Date: Tue, 26 Sep 2023 00:09:04 +0200 Subject: [PATCH] Add an example endpoint - close #25 --- CustomLibrary.ProblemDetails.sln | 6 + .../Controllers/TestController.cs | 168 ++++++++++++++++++ ...CustomLibrary.ProblemDetails.Sample.csproj | 17 ++ .../Program.cs | 19 ++ .../Properties/launchSettings.json | 31 ++++ .../Startup.cs | 62 +++++++ .../appsettings.Development.json | 8 + .../appsettings.json | 9 + 8 files changed, 320 insertions(+) create mode 100644 src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs create mode 100644 src/CustomLibrary.ProblemDetails.Sample/CustomLibrary.ProblemDetails.Sample.csproj create mode 100644 src/CustomLibrary.ProblemDetails.Sample/Program.cs create mode 100644 src/CustomLibrary.ProblemDetails.Sample/Properties/launchSettings.json create mode 100644 src/CustomLibrary.ProblemDetails.Sample/Startup.cs create mode 100644 src/CustomLibrary.ProblemDetails.Sample/appsettings.Development.json create mode 100644 src/CustomLibrary.ProblemDetails.Sample/appsettings.json diff --git a/CustomLibrary.ProblemDetails.sln b/CustomLibrary.ProblemDetails.sln index 90879bb..afccabd 100644 --- a/CustomLibrary.ProblemDetails.sln +++ b/CustomLibrary.ProblemDetails.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomLibrary.ProblemDetails", "src\CustomLibrary.ProblemDetails\CustomLibrary.ProblemDetails.csproj", "{7C1C6714-C223-4AAB-8907-880DDB971725}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomLibrary.ProblemDetails.Sample", "src\CustomLibrary.ProblemDetails.Sample\CustomLibrary.ProblemDetails.Sample.csproj", "{D2C69704-5854-47E9-86A2-496AE61809CA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {7C1C6714-C223-4AAB-8907-880DDB971725}.Debug|Any CPU.Build.0 = Debug|Any CPU {7C1C6714-C223-4AAB-8907-880DDB971725}.Release|Any CPU.ActiveCfg = Release|Any CPU {7C1C6714-C223-4AAB-8907-880DDB971725}.Release|Any CPU.Build.0 = Release|Any CPU + {D2C69704-5854-47E9-86A2-496AE61809CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D2C69704-5854-47E9-86A2-496AE61809CA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D2C69704-5854-47E9-86A2-496AE61809CA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D2C69704-5854-47E9-86A2-496AE61809CA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs new file mode 100644 index 0000000..49ecbba --- /dev/null +++ b/src/CustomLibrary.ProblemDetails.Sample/Controllers/TestController.cs @@ -0,0 +1,168 @@ +using System.Net.Mime; +using Microsoft.AspNetCore.Mvc; + +namespace CustomLibrary.ProblemDetails.Sample.Controllers; + +[ApiController] +[Route("api/[controller]")] +[Produces(MediaTypeNames.Application.Json)] +public class TestController : ControllerBase +{ + public TestController() + { + } + + [HttpGet("NotModified-Exception")] + public async Task GetExceptionNotModifiedAsync() + { + try + { + await Task.Delay(500); + throw new Exception.NotModifiedException("Not Modified"); + } + catch (Exception.NotModifiedException exc) + { + return ResponseException.NotModified(HttpContext, exc); + } + } + + [HttpGet("BadRequest-Exception")] + public async Task GetExceptionBadRequestAsync() + { + try + { + await Task.Delay(500); + throw new Exception.BadRequestException("Bad Request"); + } + catch (Exception.BadRequestException exc) + { + return ResponseException.BadRequest(HttpContext, exc); + } + } + + [HttpGet("Unauthorized-Exception")] + public async Task GetExceptionUnauthorizedAsync() + { + try + { + await Task.Delay(500); + throw new Exception.UnauthorizedException("Unauthorized"); + } + catch (Exception.UnauthorizedException exc) + { + return ResponseException.Unauthorized(HttpContext, exc); + } + } + + [HttpGet("Forbidden-Exception")] + public async Task GetExceptionForbiddenAsync() + { + try + { + await Task.Delay(500); + throw new Exception.ForbiddenException("Forbidden"); + } + catch (Exception.ForbiddenException exc) + { + return ResponseException.Forbidden(HttpContext, exc); + } + } + + [HttpGet("NotFound-Exception")] + public async Task GetExceptionNotFoundAsync() + { + try + { + await Task.Delay(500); + throw new Exception.NotFoundException("Not Found"); + } + catch (Exception.NotFoundException exc) + { + return ResponseException.NotFound(HttpContext, exc); + } + } + + [HttpGet("MethodNotAllowed-Exception")] + public async Task GetExceptionMethodNotAllowedAsync() + { + try + { + await Task.Delay(500); + throw new Exception.NotAllowedException("Method Not Allowed"); + } + catch (Exception.NotAllowedException exc) + { + return ResponseException.MethodNotAllowed(HttpContext, exc); + } + } + + [HttpGet("NotAcceptable-Exception")] + public async Task GetExceptionNotAcceptableAsync() + { + try + { + await Task.Delay(500); + throw new Exception.NotAcceptableException("Not Acceptable"); + } + catch (Exception.NotAcceptableException exc) + { + return ResponseException.NotAcceptable(HttpContext, exc); + } + } + + [HttpGet("RequestTimeout-Exception")] + public async Task GetExceptionRequestTimeoutAsync() + { + try + { + await Task.Delay(500); + throw new Exception.RequestTimeoutException("Request Timeout"); + } + catch (Exception.RequestTimeoutException exc) + { + return ResponseException.RequestTimeout(HttpContext, exc); + } + } + + [HttpGet("Conflict-Exception")] + public async Task GetExceptionConflictAsync() + { + try + { + await Task.Delay(500); + throw new Exception.ConflictException("Conflict"); + } + catch (Exception.ConflictException exc) + { + return ResponseException.Conflict(HttpContext, exc); + } + } + + [HttpGet("UnprocessableEntity-Exception")] + public async Task GetExceptionUnprocessableEntityAsync() + { + try + { + await Task.Delay(500); + throw new Exception.UnprocessableEntityException("Unprocessable Entity"); + } + catch (Exception.UnprocessableEntityException exc) + { + return ResponseException.UnprocessableEntity(HttpContext, exc); + } + } + + [HttpGet("InternalServerError-Exception")] + public async Task GetExceptionInternalServerErrorAsync() + { + try + { + await Task.Delay(500); + throw new Exception.InternalServerErrorException("Internal Server Error"); + } + catch (Exception.InternalServerErrorException exc) + { + return ResponseException.InternalServerError(HttpContext, exc); + } + } +} \ No newline at end of file diff --git a/src/CustomLibrary.ProblemDetails.Sample/CustomLibrary.ProblemDetails.Sample.csproj b/src/CustomLibrary.ProblemDetails.Sample/CustomLibrary.ProblemDetails.Sample.csproj new file mode 100644 index 0000000..2863029 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails.Sample/CustomLibrary.ProblemDetails.Sample.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/src/CustomLibrary.ProblemDetails.Sample/Program.cs b/src/CustomLibrary.ProblemDetails.Sample/Program.cs new file mode 100644 index 0000000..8e02617 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails.Sample/Program.cs @@ -0,0 +1,19 @@ +namespace CustomLibrary.ProblemDetails.Sample; + +public class Program +{ + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + Startup startup = new(builder.Configuration); + + startup.ConfigureServices(builder.Services); + + var app = builder.Build(); + + startup.Configure(app); + + app.Run(); + } +} \ No newline at end of file diff --git a/src/CustomLibrary.ProblemDetails.Sample/Properties/launchSettings.json b/src/CustomLibrary.ProblemDetails.Sample/Properties/launchSettings.json new file mode 100644 index 0000000..416a630 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails.Sample/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:36483", + "sslPort": 0 + } + }, + "profiles": { + "CustomLibrary.ProblemDetails.Sample": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5082", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/CustomLibrary.ProblemDetails.Sample/Startup.cs b/src/CustomLibrary.ProblemDetails.Sample/Startup.cs new file mode 100644 index 0000000..d703dc5 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails.Sample/Startup.cs @@ -0,0 +1,62 @@ +using Hellang.Middleware.ProblemDetails; +using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.OpenApi.Models; + +namespace CustomLibrary.ProblemDetails.Sample; + +public class Startup +{ + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + services + .AddEndpointsApiExplorer() + .AddSwaggerGen(options => + { + options.SwaggerDoc("v1", new OpenApiInfo + { + Title = "Sample", + Version = "v1", + }); + }); + + services.AddProblemDetails(); + services.AddCors(options => + { + options.AddPolicy("Sample", policy => + { + policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); + }); + }); + + services.Configure(Configuration.GetSection("Kestrel")); + services.Configure(options => options.LowercaseUrls = true); + } + + public void Configure(WebApplication app) + { + IWebHostEnvironment env = app.Environment; + + app.UseProblemDetails(); + app.UseCors("Sample"); + + app.UseSwagger(); + app.UseSwaggerUI(options => + { + options.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample v1"); + }); + + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } +} \ No newline at end of file diff --git a/src/CustomLibrary.ProblemDetails.Sample/appsettings.Development.json b/src/CustomLibrary.ProblemDetails.Sample/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/src/CustomLibrary.ProblemDetails.Sample/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/CustomLibrary.ProblemDetails.Sample/appsettings.json b/src/CustomLibrary.ProblemDetails.Sample/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/src/CustomLibrary.ProblemDetails.Sample/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}