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.
-
stackoverflow.com/questions/57768429/…Roberto Zvjerković– Roberto Zvjerković2021-03-11 12:57:38 +00:00Commented Mar 11, 2021 at 12:57
-
Does this answer your question? asp.net mvc routing with multiple optional parameters did not workChristoph Lütjen– Christoph Lütjen2021-03-11 14:47:00 +00:00Commented Mar 11, 2021 at 14:47
-
S.a. learn.microsoft.com/en-us/aspnet/core/fundamentals/…Christoph Lütjen– Christoph Lütjen2021-03-11 14:47:15 +00:00Commented Mar 11, 2021 at 14:47
-
Thank you everyone, @ChristophLütjen, I think this might help me with the concept and implementation.Alina– Alina2021-03-11 15:59:06 +00:00Commented Mar 11, 2021 at 15:59
Add a comment
|
1 Answer
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.
1 Comment
Alina
what about reactjs, is there someway to add an optional parameter there aswell?