Created
August 26, 2023 11:13
-
-
Save sunnamed434/8120a38039f556f2bad9a4f734f5e058 to your computer and use it in GitHub Desktop.
ASP.NET ProblemDetails application/problem+json
This file contains hidden or 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
services.AddControllers(configure => | |
{ | |
configure.Filters.Add<ProblemResultFilter>(); | |
}); | |
[UsedImplicitly] | |
public class ProblemResultFilter : IResultFilter | |
{ | |
[SuppressMessage("ReSharper", "MergeIntoPattern")] | |
public void OnResultExecuting(ResultExecutingContext context) | |
{ | |
if (context.Result is ObjectResult objectResult | |
&& objectResult.StatusCode == StatusCodes.Status400BadRequest) | |
{ | |
objectResult.ContentTypes.Add(KnownMediaTypes.Problem); | |
} | |
} | |
public void OnResultExecuted(ResultExecutedContext context) | |
{ | |
} | |
} | |
public static class KnownMediaTypes | |
{ | |
public const string Problem = "application/problem+json"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment