how to return Results for net7 TypedResults.Text type?

William Liu 326 Reputation points
2022-11-15T07:35:10.577+00:00

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);  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,358 questions
{count} votes

Accepted answer
  1. Rena Ni - MSFT 2,066 Reputation points
    2022-11-22T01:26:51.617+00:00

    Hi @William Liu ,

    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

    0 comments No comments

0 additional answers

Sort by: Most helpful