0

I have an API project using .NET Core 2.2 that includes a call with two parameters:

[HttpGet(nameof(VerifyCreds))]
[Route("VerifyCreds/{fName}/{lName}")]
public ActionResult<List<VerifyCredsModel>> VerifyCreds(string fName, string lName)
{
   var result = _service.VerifyCreds(fName, lName);

   return result;
}

This call works fine when called directly from the API. The parameters pass and the correct data gets passed with the parameter filters.

When generating a client in a .NET Core 3.0 project using nswag, the client code appears to see the parameters, but when the url gets created (i.e, the "urlBuilder" string), it just calls the base API method without any parameters, so it then returns all records. I've stepped through it from client to api and the parameters are not making it across the wire. I originally had them as optional parameters, but I removed all of that after reading that optional parameters are not supported (is that still true?).

Do I have to do something different when using 2 parameters? Or did I just do something wrong?

slugster
  • 49,403
  • 14
  • 95
  • 145
ewomack
  • 607
  • 9
  • 23
  • Could you add client code for this method generated by NSwag? – Alexander Goldabin Jan 28 '20 at 18:00
  • It turns out that the "Route" attribute was causing this - if I remove it everything works fine. Apparently that messes something up. I was also able to get it to work with the route and the [FromQuery] attribute added to the parameters. – ewomack Jan 28 '20 at 18:26
  • Why not just mark parameters with `[FromRoute]` attribute? May it without it NSwag cannot understand how to add parameters – Alexander Goldabin Jan 28 '20 at 18:27
  • Are the Route attributes needed or useful in some way? The calls seem to work without them and without any other attributes. – ewomack Jan 28 '20 at 18:35
  • Well they make things explicit. I think if you remove arguments from the Route your input parameters without attribute will become like annotated with `[FromQuery]`. – Alexander Goldabin Jan 28 '20 at 18:42
  • NSwag should be able to see these two as path parameters and generate correctly... smells like a big but probably it’s something small which is missing – Rico Suter Feb 12 '20 at 21:56
  • Please post the generated method – Rico Suter Feb 12 '20 at 21:56

1 Answers1

1

There were two ways I found to get around this:

a. Add the [FromQuery] attribute to the parameters:

[HttpGet(nameof(VerifyCreds))]
[Route("VerifyCreds/{fName}/{lName}")]
public ActionResult<List<VerifyCredsModel>> VerifyCreds([FromQuery]string fName, [FromQuery]string lName)
    {
       var result = _service.VerifyCreds(fName, lName);

       return result;
    }

b. Remove the route attribute:

[HttpGet(nameof(VerifyCreds))]
public ActionResult<List<VerifyCredsModel>> VerifyCreds(string fName, string lName)
    {
       var result = _service.VerifyCreds(fName, lName);

       return result;
    }

If anyone has more details on whether one is preferred, let me know. Otherwise, I went with option 2 for now.

ewomack
  • 607
  • 9
  • 23