Space
Space

Reputation: 144

Specify the name of its swagger api

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?

Upvotes: 1

Views: 1597

Answers (2)

AgentFire
AgentFire

Reputation: 9800

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.

Upvotes: 0

cl0ud
cl0ud

Reputation: 1614

 //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.

Upvotes: 2

Related Questions