Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
seungyongshim committed Feb 17, 2024
1 parent 746fcad commit fd5aa3b
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ var app = builder.Build();

app.UseExceptionHandler();
```

## Minimal API에서 FluentValidation 사용하기
* https://github.com/Carl-Hugo/FluentValidation.AspNetCore.Http
* `dotnet add package ForEvolve.FluentValidation.AspNetCore.Http`
```csharp

```
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception e
};

httpContext.Response.StatusCode = problemDetails.Status.Value;
httpContext.Response.ContentType = "application/problem+json";

await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Diagnostics;
using System.Text.Json;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.Mvc;

namespace WebApplicationMinimalApi8.ExceptionHandlers;
Expand All @@ -12,12 +14,18 @@ public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception e
var problemDetails = new ProblemDetails
{
Status = StatusCodes.Status500InternalServerError,
Title = exception.Message
Title = exception.Message,
};

httpContext.Response.StatusCode = problemDetails.Status.Value;

await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
await httpContext.Response.WriteAsJsonAsync
(
value: problemDetails,
options: new(JsonSerializerDefaults.Web),
contentType: "application/problem+json",
cancellationToken
);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/WebApplicationMinimalApi8/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{
throw new Exception("이런 저런 에러가 났군요");
})
.WithName("InternalException")6c
.WithName("InternalException")
.WithOpenApi();

app.Run();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ForEvolve.FluentValidation.AspNetCore.Http" Version="1.0.26" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions src/best-practice-asp-core-8.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31717.71
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{29C81B48-7ADA-4C89-B454-45B623039C09}"
ProjectSection(SolutionItems) = preProject
..\.editorconfig = ..\.editorconfig
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplicationMinimalApi8", "WebApplicationMinimalApi8\WebApplicationMinimalApi8.csproj", "{D82EF501-FD42-4E1F-97B6-B84C50913C2D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D82EF501-FD42-4E1F-97B6-B84C50913C2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D82EF501-FD42-4E1F-97B6-B84C50913C2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D82EF501-FD42-4E1F-97B6-B84C50913C2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D82EF501-FD42-4E1F-97B6-B84C50913C2D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EBF86A4A-E639-4F62-AAEE-EAD045707B45}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion src/src.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplicationMinimalApi8", "WebApplicationMinimalApi8\WebApplicationMinimalApi8.csproj", "{D82EF501-FD42-4E1F-97B6-B84C50913C2D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplicationMinimalApi8", "WebApplicationMinimalApi8\WebApplicationMinimalApi8.csproj", "{D82EF501-FD42-4E1F-97B6-B84C50913C2D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

0 comments on commit fd5aa3b

Please sign in to comment.