0

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.

4

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

what about reactjs, is there someway to add an optional parameter there aswell?

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.