Skip to content

Commit

Permalink
Use ContentTypes from options
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Mar 15, 2021
1 parent c284622 commit 01c1b58
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/ProblemDetails/Mvc/ProblemDetailsResultFilter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Options;
using static Hellang.Middleware.ProblemDetails.ProblemDetailsOptionsSetup;
using MvcProblemDetails = Microsoft.AspNetCore.Mvc.ProblemDetails;
using MvcProblemDetailsFactory = Microsoft.AspNetCore.Mvc.Infrastructure.ProblemDetailsFactory;
Expand All @@ -8,9 +9,10 @@ namespace Hellang.Middleware.ProblemDetails.Mvc
{
internal class ProblemDetailsResultFilter : IAlwaysRunResultFilter, IOrderedFilter
{
public ProblemDetailsResultFilter(MvcProblemDetailsFactory factory)
public ProblemDetailsResultFilter(MvcProblemDetailsFactory factory, IOptions<ProblemDetailsOptions> options)
{
Factory = factory;
Options = options.Value;
}

/// <summary>
Expand All @@ -20,6 +22,8 @@ public ProblemDetailsResultFilter(MvcProblemDetailsFactory factory)

private MvcProblemDetailsFactory Factory { get; }

private ProblemDetailsOptions Options { get; }

public void OnResultExecuting(ResultExecutingContext context)
{
// Only handle ObjectResult for now.
Expand All @@ -45,10 +49,7 @@ public void OnResultExecuting(ResultExecutingContext context)
context.Result = new ObjectResult(problemDetails)
{
StatusCode = problemDetails.Status,
ContentTypes = {
"application/problem+json",
"application/problem+xml",
},
ContentTypes = Options.ContentTypes,
};
}

Expand Down
3 changes: 0 additions & 3 deletions src/ProblemDetails/ProblemDetailsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,8 @@ private async Task WriteProblemDetails(HttpContext context, MvcProblemDetails de
{
StatusCode = details.Status ?? context.Response.StatusCode,
ContentTypes = Options.ContentTypes,
DeclaredType = details.GetType(),
};

result.ContentTypes = Options.ContentTypes;

await Executor.ExecuteAsync(actionContext, result);

await context.Response.CompleteAsync();
Expand Down

0 comments on commit 01c1b58

Please sign in to comment.