We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; namespace MinimalApis.Extensions.Filters; /// <summary> /// Defines an endpoint filter that modifies <see cref="ProblemHttpResult"/> instances returned by route handler delegates /// using the <see cref="IProblemDetailsService"/>. /// </summary> public class ProblemDetailsServiceEndpointFilter : IEndpointFilter { public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) => await next(context) switch { ProblemHttpResult problemHttpResult => new ProblemDetailsServiceAwareResult(problemHttpResult.StatusCode, problemHttpResult.ProblemDetails), ProblemDetails problemDetails => new ProblemDetailsServiceAwareResult(null, problemDetails), { } result => result, null => null }; private class ProblemDetailsServiceAwareResult : IResult, IValueHttpResult, IValueHttpResult<ProblemDetails> { private readonly int? _statusCode; public ProblemDetailsServiceAwareResult(int? statusCode, ProblemDetails problemDetails) { _statusCode = statusCode ?? problemDetails.Status; Value = problemDetails; } public ProblemDetails Value { get; } object? IValueHttpResult.Value => Value; public async Task ExecuteAsync(HttpContext httpContext) { if (httpContext.RequestServices.GetService<IProblemDetailsService>() is IProblemDetailsService problemDetailsService) { if (_statusCode is { } statusCode) { httpContext.Response.StatusCode = statusCode; } await problemDetailsService.WriteAsync(new ProblemDetailsContext { HttpContext = httpContext, ProblemDetails = Value }); } } } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: