Skip to content
New issue

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

ProblemDetails middleware is ignored when returning StatusCode(409, error); #9

Closed
PureKrome opened this issue Oct 9, 2018 · 1 comment

Comments

@PureKrome
Copy link

Hi πŸ‘‹

NOTE: this issue is very similar to #2 .

When trying to return a StatusCode result, the middleware isn't invoked iff I provide a model (in this case, a ProblemDetails instance).

βœ… Works.. (this code taken from your samples-code)..

[HttpGet("status/{statusCode}")]
public IActionResult Status([FromRoute] int statusCode)
{
    return StatusCode(statusCode);
}
  • Content-Type: "application/problem+json"

❌ This code fails to call the middleware.

[HttpGet("conflict")]
public IActionResult ConflictCheck()
{
    var error = new ProblemDetails
    {
        Type = "https://httpstatuses.com/409",
        Title = "Agent was already modified.",
        Status = StatusCodes.Status409Conflict,
        Instance = "/test/conflict",
        Detail = "agent was already modified after you retrieved the latest data. So you would then override the most recent copy. As such, you will need to refresh the page (to get the latest data) then modify that, if required."
    };

    return StatusCode(409, error);
}
  • Content-Type: "application/json"

The reason (which is why this is similar to #2) is because, for the 2nd (not working) example ... when it calls if (Options.IsProblem(context)) (ref: your source code) that returns false. The reason why it returns false is this:

image

My guess is that, in the first (working) example, there is no model .. so there is nothing to write ... so no content-type was set. Where-as in the second example there is a model provided, so it must write the content-type out before the ProblemDetails middeware is invoked?

I'm not sure what the best solution here, is?

@khellang
Copy link
Owner

khellang commented Oct 9, 2018

This is really a duplicate of #5. The best solution is to follow the workaround there, or wait for ASP.NET Core 2.2, which implicitly adds the correct content type for ProblemDetails types πŸ˜„

@khellang khellang closed this as completed Oct 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants