So I have a generic URL that is used in multiple places on my React app to send a call to my asp.net core api. Now due to a change requrement, I need to use the same url but in one specific scenario, I need to send an additional query parameter. I want to use the same url without affecting the current implementation. Please suggest or direct me to the proper way in asp.net core to implement this.
Asked
Active
Viewed 628 times
0
-
https://stackoverflow.com/questions/57768429/c-sharp-web-api-optional-parameters-in-query-string – Roberto Zvjerković Mar 11 '21 at 12:57
-
Does this answer your question? [asp.net mvc routing with multiple optional parameters did not work](https://stackoverflow.com/questions/37582394/asp-net-mvc-routing-with-multiple-optional-parameters-did-not-work) – Christoph Lütjen Mar 11 '21 at 14:47
-
S.a. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0#rtr – Christoph Lütjen Mar 11 '21 at 14:47
-
Thank you everyone, @ChristophLütjen, I think this might help me with the concept and implementation. – Alina Mar 11 '21 at 15:59
1 Answers
-1
About the optional query parameters in WebApi, the usual practice is to add ?
, but it can only correspond to the order of the parameters one-to-one.
[Route("get/{a?}/{b?}")]
public IActionResult get( string a, string b)
{
return Ok(new { a,b});
}
If you use multiple [Route]
to achieve the same purpose as above. It can match the corresponding value according to the data type.
[Route("get/{a}")]
[Route("get/{a}/{b}")]
[Route("get/{b:int}")]
public IActionResult get( string a, int b)
{
return Ok(new { a,b});
}
About other rules, you can refer to the document which Christoph Lütjen provided in comment.

Karney.
- 4,803
- 2
- 7
- 11
-
what about reactjs, is there someway to add an optional parameter there aswell? – Alina Mar 12 '21 at 15:46