3

I'm trying to generate a swagger specification with NSwag.MSbuild but whenever I do it throws me this message:

The method 'get' on path '/api/Account' is registered multiple times

Now the problem is that my methods are route-less as shown below with some examples of the controller

        [HttpPost]
        [HttpGet]
        [AllowAnonymous]
        public IActionResult ExternalRegister(string provider, string returnUrl = null)

        [HttpGet]
        public IActionResult AddLogin(string provider, string returnUrl)

        [HttpGet]
        [AllowAnonymous]
        public ActionResult SignUpConfig()

I understand why it does this but what I don't understand is that doing the same thing in NSwag Studio works, the command I use is $(NSwagExe_Core22) webapi2swagger is there an option so that it generates successfully like NSwag Studio?

Dale K
  • 25,246
  • 15
  • 42
  • 71
user2696482
  • 63
  • 2
  • 8

6 Answers6

4

In a WebAPI if you have more than one HttpGet or HttpPost etc you should add Route Attribute to distinguish them.

Add HttpGet["{name}"]

Dale K
  • 25,246
  • 15
  • 42
  • 71
mesut
  • 2,099
  • 3
  • 23
  • 35
  • It does make sense but the problem is that its a big WebApi i'm not sure how those are handled there must be a reason for those to not having a route but Nswag Studio does succede to generate the spec with this configuration i don't know what it does differently – user2696482 Mar 03 '20 at 08:18
  • I'm not sure, but maybe uses reflection through actions of all controllers. – mesut Mar 03 '20 at 08:28
1

What helped me in this situation was to set the Route Attribute like this:

[Route("SignUpConfig")] ,[Route("AdLogin")]
Dale K
  • 25,246
  • 15
  • 42
  • 71
Andre Fritzsche
  • 126
  • 2
  • 11
1

Turns out you don't have to specify the routes if you don't want to it has something to do with the Default Url Template: /DefaultUrlTemplate:"{controller}/{action}/{id?}" adding {action} solved it for me

user2696482
  • 63
  • 2
  • 8
1

In my case, I had already added custom [Route("")] attributes to all the paths. The problem was I had two public helper methods in the controller which NSwag identified as GET methods. Simply making them private (which they should have been anyway) made the problem go away...

Dale K
  • 25,246
  • 15
  • 42
  • 71
D2TheC
  • 2,203
  • 20
  • 23
0

In my case I had

[HttpGet, ActionName("Stuff")]
public async Task<Stuff> GetStuff(long byId, string color)
{
    /* Do things one way */
}

[HttpGet, ActionName("Stuff")]
public async Task<Stuff> GetStuff(string byName, string color)
{
    /* Do things another way */
}

The problem was that there were two identically named routes that take in different parameters. This is an overload situation that ASP.NET seems to be perfectly fine with but apparently blows NSwag's mind.

Because this was in legacy code, renaming the methods was not an option for me so I created a single method with optional parameters like so:

[HttpGet, ActionName("Stuff")]
public async Task<Stuff> GetStuff(string color, long? byId = null, string byName = null )
{
    if (byId != null)
    {
        /* Do things one way */
    }
    else
    {
       /* Do things another way */
    }
}
Bernard Hymmen
  • 825
  • 1
  • 7
  • 14
0

If your controller is decorated with [Route("[controller]")]

then you need you specify separate names

HttpGet("get1") and HttpGet("get2")

Else it will pick if decoration contains action name it it like

Route("[controller]/[action]") or from default route {controller}/{action}/{id?}

Dale K
  • 25,246
  • 15
  • 42
  • 71
Uttam Ughareja
  • 842
  • 2
  • 12
  • 21