1

I have a controller that is named User in which I have two GET methods to retrieve the user's information either by his mail, GetUserByMail, or by his mobile number, GetUserByMobile.

The problem is that when I generate my Swagger documentation I have both GET but not their names. They are both called "User" with their respective parameters "mail" or "mobile".

How do I specify the name of my API via the C# attributes for Swagger to include it correctly in the documentation?

d219
  • 2,707
  • 5
  • 31
  • 36
Space
  • 124
  • 1
  • 13
  • 1
    Are you specifying the route name annotation ? Like this _[HttpGet("GetUserByMail")]_ If you dont, swagger will recognize both as _get_ – Jaime Yule Oct 22 '18 at 14:36
  • 1
    https://stackoverflow.com/questions/47576436/swagger-net-shows-controller-name-instead-of-endpoint-method – CodeCaster Oct 22 '18 at 14:39
  • 1
    https://stackoverflow.com/questions/43831986/show-name-from-route-in-swagger-documentation-using-swashbuckle – CodeCaster Oct 22 '18 at 14:40

2 Answers2

1
 //This is important
[Route("api/[controller]/[action]")]
public class PingController : Controller
{

    [HttpGet]
    public IActionResult Ping()
    {
        return Ok("Pong");
    }


}

The [action] indicates to swagger to use the method name.

cl0ud
  • 1,524
  • 11
  • 27
0

You have to explicitly specify the action methods' name, at which they will be accessed in your API:

[RoutePrefix("api/user")]
public sealed class UserController : ApiController
{
    [Route("get-user-by-mail/{email}")]
    public object GetUserByMail(string email) { }

    [Route("get-user-by-mobile/{mobile}")]
    public object GetUserByMail(string mobile) { }
}

If you put your RouteAttribute on the whole class, it will create a single route for all the methods within it.

AgentFire
  • 8,944
  • 8
  • 43
  • 90