CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.05k stars 172 forks source link

[Question] What is the best way to return a HTTP Api response using Carter? #354

Closed jeffward01 closed 1 month ago

jeffward01 commented 1 month ago

Hello,

I am curious what the recommended method is to return http response data using Carter?

Carter Examples use this Negotiate():

 var result = req.Validate<Actor>(actor);

                if (!result.IsValid)
                {
                    res.StatusCode = 422;

                   // Is this the recommended way?
                    await res.Negotiate(result.GetFormattedErrors());
                    return;
                }

However, this also works, and I have seen some examples online of other user's using carter, while using this response type from Microsoft.AspNetCore.Http:

         var validationResult = await req.ValidateAsync(command);

                if (!validationResult.IsValid)
                {

                    // Or is this fine also?
                    return Results.ValidationProblem(validationResult.GetValidationProblems(),
                        statusCode: 422);
                }

Is there a difference between the two ways of returning an HTTP Response? Using Negotiate(), versus using the Results.XXX from Microsoft.AspNetCore.Http

Thanks all

jchannon commented 1 month ago

Negotiate is a way to tell Carter to look for a ResponseNegotiator to represent the result data as the conte t type the client has asked for in the Accept header.

I believe using Result.Ok(myDataObject) always forces it to use the json serializer.

You can also just do return myDataObject too

On Thu, 30 May 2024 at 18:05, Jeff Ward @.***> wrote:

Hello,

I am curious what the recommended method is to return http response data using Carter?

Carter Examples use this Negotiate():

var result = req.Validate(actor);

            if (!result.IsValid)
            {
                res.StatusCode = 422;

               // Is this the recommended way?
                await res.Negotiate(result.GetFormattedErrors());
                return;
            }

However, this also works, and I have seen some examples online of other user's using carter, while using this response type from Microsoft.AspNetCore.Http:

     var validationResult = await req.ValidateAsync(command);

            if (!validationResult.IsValid)
            {

                // Or is this fine also?
                return Results.ValidationProblem(validationResult.GetValidationProblems(),
                    statusCode: 422);
            }

Is there a difference between the two ways of returning an HTTP Response? Using Negotiate(), versus using the Results.XXX from Microsoft.AspNetCore.Http

Thanks all

— Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/354, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJUUQ3XHHPU2QKEE7RLZE5L67AVCNFSM6AAAAABIRJYSW6VHI2DSMVQWIX3LMV43ASLTON2WKOZSGMZDMMJUHEZDKNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>