Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

Commit 4f66344

Browse files
authored
Merge pull request #27 from AngeloDotNet/25-add-an-example-endpoint-for-each-available-status-code
Add an example endpoint - close #25
2 parents 08cd145 + ab7b3f6 commit 4f66344

File tree

8 files changed

+320
-0
lines changed

8 files changed

+320
-0
lines changed

CustomLibrary.ProblemDetails.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 17.0.31903.59
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomLibrary.ProblemDetails", "src\CustomLibrary.ProblemDetails\CustomLibrary.ProblemDetails.csproj", "{7C1C6714-C223-4AAB-8907-880DDB971725}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomLibrary.ProblemDetails.Sample", "src\CustomLibrary.ProblemDetails.Sample\CustomLibrary.ProblemDetails.Sample.csproj", "{D2C69704-5854-47E9-86A2-496AE61809CA}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{7C1C6714-C223-4AAB-8907-880DDB971725}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{7C1C6714-C223-4AAB-8907-880DDB971725}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{7C1C6714-C223-4AAB-8907-880DDB971725}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{D2C69704-5854-47E9-86A2-496AE61809CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{D2C69704-5854-47E9-86A2-496AE61809CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{D2C69704-5854-47E9-86A2-496AE61809CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{D2C69704-5854-47E9-86A2-496AE61809CA}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using System.Net.Mime;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace CustomLibrary.ProblemDetails.Sample.Controllers;
5+
6+
[ApiController]
7+
[Route("api/[controller]")]
8+
[Produces(MediaTypeNames.Application.Json)]
9+
public class TestController : ControllerBase
10+
{
11+
public TestController()
12+
{
13+
}
14+
15+
[HttpGet("NotModified-Exception")]
16+
public async Task<IActionResult> GetExceptionNotModifiedAsync()
17+
{
18+
try
19+
{
20+
await Task.Delay(500);
21+
throw new Exception.NotModifiedException("Not Modified");
22+
}
23+
catch (Exception.NotModifiedException exc)
24+
{
25+
return ResponseException.NotModified(HttpContext, exc);
26+
}
27+
}
28+
29+
[HttpGet("BadRequest-Exception")]
30+
public async Task<IActionResult> GetExceptionBadRequestAsync()
31+
{
32+
try
33+
{
34+
await Task.Delay(500);
35+
throw new Exception.BadRequestException("Bad Request");
36+
}
37+
catch (Exception.BadRequestException exc)
38+
{
39+
return ResponseException.BadRequest(HttpContext, exc);
40+
}
41+
}
42+
43+
[HttpGet("Unauthorized-Exception")]
44+
public async Task<IActionResult> GetExceptionUnauthorizedAsync()
45+
{
46+
try
47+
{
48+
await Task.Delay(500);
49+
throw new Exception.UnauthorizedException("Unauthorized");
50+
}
51+
catch (Exception.UnauthorizedException exc)
52+
{
53+
return ResponseException.Unauthorized(HttpContext, exc);
54+
}
55+
}
56+
57+
[HttpGet("Forbidden-Exception")]
58+
public async Task<IActionResult> GetExceptionForbiddenAsync()
59+
{
60+
try
61+
{
62+
await Task.Delay(500);
63+
throw new Exception.ForbiddenException("Forbidden");
64+
}
65+
catch (Exception.ForbiddenException exc)
66+
{
67+
return ResponseException.Forbidden(HttpContext, exc);
68+
}
69+
}
70+
71+
[HttpGet("NotFound-Exception")]
72+
public async Task<IActionResult> GetExceptionNotFoundAsync()
73+
{
74+
try
75+
{
76+
await Task.Delay(500);
77+
throw new Exception.NotFoundException("Not Found");
78+
}
79+
catch (Exception.NotFoundException exc)
80+
{
81+
return ResponseException.NotFound(HttpContext, exc);
82+
}
83+
}
84+
85+
[HttpGet("MethodNotAllowed-Exception")]
86+
public async Task<IActionResult> GetExceptionMethodNotAllowedAsync()
87+
{
88+
try
89+
{
90+
await Task.Delay(500);
91+
throw new Exception.NotAllowedException("Method Not Allowed");
92+
}
93+
catch (Exception.NotAllowedException exc)
94+
{
95+
return ResponseException.MethodNotAllowed(HttpContext, exc);
96+
}
97+
}
98+
99+
[HttpGet("NotAcceptable-Exception")]
100+
public async Task<IActionResult> GetExceptionNotAcceptableAsync()
101+
{
102+
try
103+
{
104+
await Task.Delay(500);
105+
throw new Exception.NotAcceptableException("Not Acceptable");
106+
}
107+
catch (Exception.NotAcceptableException exc)
108+
{
109+
return ResponseException.NotAcceptable(HttpContext, exc);
110+
}
111+
}
112+
113+
[HttpGet("RequestTimeout-Exception")]
114+
public async Task<IActionResult> GetExceptionRequestTimeoutAsync()
115+
{
116+
try
117+
{
118+
await Task.Delay(500);
119+
throw new Exception.RequestTimeoutException("Request Timeout");
120+
}
121+
catch (Exception.RequestTimeoutException exc)
122+
{
123+
return ResponseException.RequestTimeout(HttpContext, exc);
124+
}
125+
}
126+
127+
[HttpGet("Conflict-Exception")]
128+
public async Task<IActionResult> GetExceptionConflictAsync()
129+
{
130+
try
131+
{
132+
await Task.Delay(500);
133+
throw new Exception.ConflictException("Conflict");
134+
}
135+
catch (Exception.ConflictException exc)
136+
{
137+
return ResponseException.Conflict(HttpContext, exc);
138+
}
139+
}
140+
141+
[HttpGet("UnprocessableEntity-Exception")]
142+
public async Task<IActionResult> GetExceptionUnprocessableEntityAsync()
143+
{
144+
try
145+
{
146+
await Task.Delay(500);
147+
throw new Exception.UnprocessableEntityException("Unprocessable Entity");
148+
}
149+
catch (Exception.UnprocessableEntityException exc)
150+
{
151+
return ResponseException.UnprocessableEntity(HttpContext, exc);
152+
}
153+
}
154+
155+
[HttpGet("InternalServerError-Exception")]
156+
public async Task<IActionResult> GetExceptionInternalServerErrorAsync()
157+
{
158+
try
159+
{
160+
await Task.Delay(500);
161+
throw new Exception.InternalServerErrorException("Internal Server Error");
162+
}
163+
catch (Exception.InternalServerErrorException exc)
164+
{
165+
return ResponseException.InternalServerError(HttpContext, exc);
166+
}
167+
}
168+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\CustomLibrary.ProblemDetails\CustomLibrary.ProblemDetails.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace CustomLibrary.ProblemDetails.Sample;
2+
3+
public class Program
4+
{
5+
public static void Main(string[] args)
6+
{
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
Startup startup = new(builder.Configuration);
10+
11+
startup.ConfigureServices(builder.Services);
12+
13+
var app = builder.Build();
14+
15+
startup.Configure(app);
16+
17+
app.Run();
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:36483",
8+
"sslPort": 0
9+
}
10+
},
11+
"profiles": {
12+
"CustomLibrary.ProblemDetails.Sample": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5082",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"IIS Express": {
23+
"commandName": "IISExpress",
24+
"launchBrowser": true,
25+
"launchUrl": "swagger",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Hellang.Middleware.ProblemDetails;
2+
using Microsoft.AspNetCore.Server.Kestrel.Core;
3+
using Microsoft.OpenApi.Models;
4+
5+
namespace CustomLibrary.ProblemDetails.Sample;
6+
7+
public class Startup
8+
{
9+
public Startup(IConfiguration configuration)
10+
{
11+
Configuration = configuration;
12+
}
13+
14+
public IConfiguration Configuration { get; }
15+
16+
public void ConfigureServices(IServiceCollection services)
17+
{
18+
services.AddControllers();
19+
services
20+
.AddEndpointsApiExplorer()
21+
.AddSwaggerGen(options =>
22+
{
23+
options.SwaggerDoc("v1", new OpenApiInfo
24+
{
25+
Title = "Sample",
26+
Version = "v1",
27+
});
28+
});
29+
30+
services.AddProblemDetails();
31+
services.AddCors(options =>
32+
{
33+
options.AddPolicy("Sample", policy =>
34+
{
35+
policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
36+
});
37+
});
38+
39+
services.Configure<KestrelServerOptions>(Configuration.GetSection("Kestrel"));
40+
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
41+
}
42+
43+
public void Configure(WebApplication app)
44+
{
45+
IWebHostEnvironment env = app.Environment;
46+
47+
app.UseProblemDetails();
48+
app.UseCors("Sample");
49+
50+
app.UseSwagger();
51+
app.UseSwaggerUI(options =>
52+
{
53+
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample v1");
54+
});
55+
56+
app.UseRouting();
57+
app.UseEndpoints(endpoints =>
58+
{
59+
endpoints.MapControllers();
60+
});
61+
}
62+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)