-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from MrDave1999/feat/issue-49
* feat: Add an endpoint filter to translate the Result object to Microsoft.AspNetCore.Http.IResult * docs: Add an example on how to use custom endpoint filter
- Loading branch information
Showing
2 changed files
with
38 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#if NET7_0_OR_GREATER | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace SimpleResults; | ||
|
||
/// <summary> | ||
/// Translates the Result object to an implementation of <see cref="IResult"/>. | ||
/// <para>Result object can be:</para> | ||
/// <list type="bullet"> | ||
/// <item><see cref="Result{T}"/></item> | ||
/// <item><see cref="ListedResult{T}"/></item> | ||
/// <item><see cref="PagedResult{T}"/></item> | ||
/// <item><see cref="Result"/></item> | ||
/// <item>A subtype of <see cref="ResultBase"/>.</item> | ||
/// </list> | ||
/// </summary> | ||
public class TranslateResultToHttpResultFilter : IEndpointFilter | ||
{ | ||
/// <summary> | ||
/// Translates the Result object into a corresponding HTTP status code. | ||
/// This translation occurs after an endpoint handler is executed. | ||
/// </summary> | ||
public async ValueTask<object> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) | ||
{ | ||
object endpointHandlerResult = await next(context); | ||
return endpointHandlerResult is ResultBase result ? | ||
result.TranslateToHttpResult() : | ||
endpointHandlerResult; | ||
} | ||
} | ||
#endif |