With the following code, Swaggger UI shows the RegistrationInfo model but not the UserInfo model.
How do I get that to generate?
[Produces("application/json")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api")]
public class UserController : Controller
{
[HttpPost("RegisterUser")]
public IActionResult RegisterUser([FromBody] RegistrationInfo info)
{
UserInfo data = UserData.RegisterUser(info);
if (data != null)
{
return Ok(data);
}
return NoContent();
}
}
IActionResult type. The IActionResult return type is appropriate when multiple ActionResult return types are possible in an action. The ActionResult types represent various HTTP status codes. Any non-abstract class deriving from ActionResult qualifies as a valid return type.
AddSwaggerGen is an extension method to add swagger services to the collection. To configure Swagger, you invoke the method SwaggerDoc. Passing an Info object, you can define the title, description, contact information, and more in code file Startup. cs. Next open Startup.
The {documentName} refers to the name you specify in the AddSwaggerGen() method. The following code uses myapi as the name for a swagger document. builder.Services.AddSwaggerGen(options => options.SwaggerDoc("myapi", new OpenApiInfo { Title = "My API", Version = "v1" }) );
AspNetCore. Swagger: a Swagger object model and middleware to expose SwaggerDocument objects as JSON endpoints. Swashbuckle. AspNetCore. SwaggerGen: a Swagger generator that builds SwaggerDocument objects directly from your routes, controllers, and models.
You need to use the ProducesResponseType
attribute. Change your controller to this:
[Produces("application/json")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api")]
public class UserController : Controller
{
[ProducesResponseType(typeof(UserInfo), StatusCodes.Status200OK)]
[HttpPost("RegisterUser")]
public IActionResult RegisterUser([FromBody] RegistrationInfo info)
{
UserInfo data = UserData.RegisterUser(info);
if (data != null)
{
return Ok(data);
}
return NoContent();
}
}
See more here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With