Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Swagger not showing parameters in UI and JSON, even tho my method has parameters, This particularly happens when I add the [FromBody] tag
swagger UI no parameters JSON file no parameters
The action method:

    [HttpPost("Action")]
    public IActionResult Action([FromBody] string message)
        return Ok(message);

I used fresh Asp.net core 3.1 and 2.2 web app with API template to test this, I configured it exactly like the documnetaiton
ConfigureServices:

services.AddMvc();
services.AddSwaggerGen(c =>
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });

Configure:

 app.UseSwagger();
   app.UseSwaggerUI(c =>
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");

I had a similar problem on AspNetCore 5.0, where "complex" types would be completely ignored, both as parameters and output types. The problem was caused by switching input/output formatters to a different JSON serializer (legacy code, due to newtonsoft being slow in certain situations). The new swashbuckle can work either with System.Text.Json or Newtonsoft, but not a 3rd solution. So check .AddMvcOptions() in ConfigureServices, if there is anything done with InputFormatters or OutputFormatters, that might be the culprit

You should look into this code sample as you are working with OpenAPI 3.0 : http://petstore.swagger.io:8080/

OpenAPI 3.0 provides the requestBody keyword to describe request bodies. It distinguish the payload from parameters (such as query string and PATH). The requestBody is more flexible in that it lets you consume different media types, such as JSON, XML, form data, plain text, and others, and use different schemas for different media types:

https://swagger.io/docs/specification/describing-request-body/

That is default UI and if you click "Try it out" , the sample value will auto fill Request body area to help create a test request body .

This isn't a question about Swagger, it's a question about ASP.NET MVC's ApiController functionality. – Benjamin Nolan Jan 10, 2021 at 6:10

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.