Home > front end >  "Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0" - Is [HttpGet] a
"Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0" - Is [HttpGet] a

Time:12-30

I'm watching a .Net 5.0 tutorial where the instructor defined two endpoints without [HttpGet] attribute (it works in his video):

enter image description here

I have a .Net 7.0 project. And I get the following message for the following two endpoints: "Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0"

    [Route("{id:int}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public ActionResult<Dto> DoSomething

    [Route("{name}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public ActionResult<DTO> DoSomething2

If I add [HttpGet] it works. So this a required attribute for Swagger 3.0?

Edit: I just executed the plain REST calls in a browser. And they worked. But the Swagger UI throws the error mentioned above. So it seems to be a Swagger problem.

CodePudding user response:

It's not a Swagger problem. It's a Swashbuckle problem, which implements the scanning code for .NET controllers. Even then, it's not so much a problem as it's just the way the Swagger UI was designed. I believe the original reason is that finding methods with [HttpGet] is much easier than looking for conventional naming (such as methods starting with Get) or other potentially error-prone ways of scanning.

So, in that sense, yes you must attribute your controllers with [HttpGet].

  • Related