Giter Club home page Giter Club logo

Comments (3)

lurumad avatar lurumad commented on July 30, 2024 2

Thanks @khellang

Was my fault, I've configured ApiBehaviorOptions and I don't need the ActionFilter and works together:

public static IServiceCollection AddProblemDetails(this IServiceCollection services) =>
    services.Configure<ApiBehaviorOptions>(options =>
    {
        options.InvalidModelStateResponseFactory = context =>
        {
            var problemDetails = new ValidationProblemDetails(context.ModelState)
            {
                Instance = context.HttpContext.Request.Path,
                Status = StatusCodes.Status400BadRequest,
                Type = $"https://httpstatuses.com/400",
                Detail = "Please refer to the errors property for additional details."
            };
            return new BadRequestObjectResult(problemDetails)
            {
                ContentTypes = { "application/problem+json", "application/problem+xml" }
            };
        };
    });

I have to say Hellang.Middleware.ProblemDetails is awesome, great work!!!

Regards!

from middleware.

khellang avatar khellang commented on July 30, 2024

Hi!

The assumption is that if Content-Length and Content-Type has been set, the response body has already been written by somone else. They're kinda redundant since we also check HasStarted, but it follows the same rules as the exception handler middleware, so I just kept it for consistency.

Someone else, in this case, is MVC's object result executor. It has already set the content type and written to the body by the time the problem details middleware is reached on the way out. This means it's too late for the middleware to write anything.

In this case it shouldn't matter whether the middleware or MVC's filter writes the response, since it's a ValidationProblemDetails instance. The JSON representation should be the same.

If you really need the MW to handle the response, you can throw a ProblemDetailsException and it should be handled automatically.

from middleware.

khellang avatar khellang commented on July 30, 2024

Nice. Glad you got it working! Incidentally, I just sent a PR to MVC to copy over the result status code to the problem details object, so from 2.2, you don't have to set it explicitly; aspnet/Mvc#8118

from middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.