0

I'm working on an endpoint which looks like this:

GET {{url}}/v1.0/GroupsInfo/StuffToGet/1/2/hea

where the 2 integers are certain ids and the text on the end is a search term.

I want the last parameter (the search text) to be optional. I have got that working using the following, but it does not appear as mandatory in Swagger:

[Route("[action]/{catId:int}/{dogId:int}/{search?}")]
public IActionResult StuffToGet(int catId, int dogId, string search)
{
    // do stuff
}

Swagger is ignoring the question mark on the end of the Search parameter in the route constraint.

Is this a known issue, or do I need to right some custom code to get Swagger to recognise that optional flag?

2
  • It will be working. Do you have any problem? Commented Jul 20, 2021 at 0:53
  • @Serge Sorry. I need to re-phrase my question. You are correct. It does work. However, it does not work with Swagger. Swagger makes it a mandatory parameter. Will edit question now. Commented Jul 20, 2021 at 1:02

2 Answers 2

1

your variant will be working, but if you have a problem with swagger try this

[Route("[action]/{catId:int}/{dogId:int}]
[Route("[action]/{catId:int}/{dogId:int}/{search}")]
public IActionResult StuffToGet(int catId, int dogId, string search)
{
    // do stuff
}

or maybe this


[Route("StuffToGet/{catId:int}/{dogId:int}/{search}")]
public IActionResult StuffToGet(int catId, int dogId, string search)
{
    // do stuff
}
[Route("StaffToGet/{catId:int}/{dogId:int}]
public IActionResult StuffToGet(int catId, int dogId)
{
   return StuffToGet(catId, dogId,string.empty);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I did try that. It added a second textbox for the search variable in the Swagger interface, but it kept the mandatory textbox for the search variable as well.
Sorry, I got that wrong, it created a whole new endpoint, but the "try it out" section still contained the mandatory search parameter textbox, even though it was not in the actual url signature for that endpoint.
Yeah. That's it. A pity you have to create a whole new endpoint. But it works.
Yes, sometimes swagger makes strange stuff.
1

Create two actions with similar routes, but one without the search parameter. Both actions call the same business layer code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.