question

WilliamLiu-0692 avatar image
0 Votes"
WilliamLiu-0692 asked RenaNi-MSFT answered

how to return Results for net7 TypedResults.Text type?

Per the MSDN document saying, you can use the following code in net 7. It can help to generate different return type in swagger document according this blog saying.

 Results<Ok, BadRequest> result = TypedResults.Ok();

My question is, if I have TypedResults.Text with different StatusCodes, which means it will return 200 or 400 result. How to specify the generic Results type for this return?
I tried the following code, but it just returns the BadRequest type in swagger without the 200 retrun type.

 Results<ContentHttpResult, BadRequest> result = bResult
     ? TypedResults.Text("Ok")
     : TypedResults.Text("Bad", statusCode:StatusCodes.Status400BadRequest);




dotnet-aspnet-core-general
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @WilliamLiu-0692, what is your bResult?

0 Votes 0 ·

It`s just a boolean value received from other place. Then this code will response "Ok" or "Bad" depending on this boolean value.

0 Votes 0 ·

Hi @WilliamLiu-0692 , yes, of course it is a Boolean value. If bResult is true it will get response "OK" with 200 status code, otherwise it will get response "bad" with 400 status code. Do you mean you want to display this two results in the sample of the Responses in swagger like the following picture from your shared blog?


261273-image.png


0 Votes 0 ·
image.png (62.3 KiB)
Show more comments

1 Answer

RenaNi-MSFT avatar image
0 Votes"
RenaNi-MSFT answered

Hi @WilliamLiu-0692,

You need change your code like below to achieve your requirement:

 app.MapGet("/foo", Foo).Produces<ContentHttpResult>().Produces<BadRequest>();


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best Regards,
Rena


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.