locked
Optional Parameter RRS feed

  • Question

  • User2074368383 posted

    Hi

    I have a api WS with this route: 

    [HttpGet, HttpHead, Route("api/app/races/{eventCupId}/{disciplineId}/{ownerTypeId?}/{categoryId?}/{ageCategoryId?}")]
    public IHttpActionResult Races(int eventCupId, String disciplineId, String ownerTypeId = null, String categoryId = null, String ageCategoryId = null)

    The parameters ownerTypeId, categoryId and ageCategoryId are optional. Now i have to call this WS passing categoryId (4° parameter), but NOT ownerTypeId (3° parameter).

    How can I call this WS? Is possible jump a parameter or i have to pass a "fake parameter" and check it in C# code?

    From C# code:  var response = await client.GetAsync(String.Format("api/app/races/{0}/{1}//{2}/{3}", "40daecda-7087-4b99-9eae-70f88fefc351", "TE", null, "BO"));

    Someone can help me?

    THANKS

    Tuesday, January 15, 2019 10:47 AM

Answers

  • User36583972 posted

    Hi gianni11,

    gianni11

    Ok for POST or PUT, but i don't know, how use querystring in a webapi WS. Can you write an example?

    You can refer the following sample:

            //http://localhost/api/races?eventCupId=1&disciplineId=2&ownerTypeId=hello&ageCategoryId=23
            //http://localhost/api/races?eventCupId=1&disciplineId=2&ownerTypeId=hello&categoryId=009
            //http://localhost/api/races?eventCupId=1&disciplineId=2&categoryId=009&ageCategoryId=23
            [HttpGet]
            //[Route("api/races/{eventCupId}/{disciplineId?}/{ownerTypeId?}/{categoryId?}/{ageCategoryId?}")]
            [Route("api/races")]
            public IHttpActionResult Races(int eventCupId, string disciplineId, string ownerTypeId = null, string categoryId = null, string ageCategoryId = null)
            {
                string message = "[ eventCupId: " + eventCupId.ToString() + ", disciplineId:" + disciplineId + ", ownerTypeId: " + ownerTypeId + ", categoryId: " + categoryId + ", ageCategoryId " + ageCategoryId + " ]";
    
                return Ok(message);
            }
    


    Besides, I suggest you can pass a object which contains these fields. You can check the fields in Web API Function.

    The following links for your reference.

    How to pass json POST data to Web API method as an object:
    https://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-an-object

    Sending C# object to webapi controller
    https://stackoverflow.com/questions/19610883/sending-c-sharp-object-to-webapi-controller

    It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread.

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 8:26 AM

All replies

  • User475983607 posted

    This is a design bug. 

    Use a querystring to implement optional parameters (not routing) or POST the inputs. 

    Tuesday, January 15, 2019 3:47 PM
  • User2074368383 posted

    Hi mgebhard

    Ok for POST or PUT, but i don't know, how use querystring in a webapi WS. Can you write an example?

    Thanks

    Wednesday, January 16, 2019 8:01 AM
  • User36583972 posted

    Hi gianni11,

    gianni11

    Ok for POST or PUT, but i don't know, how use querystring in a webapi WS. Can you write an example?

    You can refer the following sample:

            //http://localhost/api/races?eventCupId=1&disciplineId=2&ownerTypeId=hello&ageCategoryId=23
            //http://localhost/api/races?eventCupId=1&disciplineId=2&ownerTypeId=hello&categoryId=009
            //http://localhost/api/races?eventCupId=1&disciplineId=2&categoryId=009&ageCategoryId=23
            [HttpGet]
            //[Route("api/races/{eventCupId}/{disciplineId?}/{ownerTypeId?}/{categoryId?}/{ageCategoryId?}")]
            [Route("api/races")]
            public IHttpActionResult Races(int eventCupId, string disciplineId, string ownerTypeId = null, string categoryId = null, string ageCategoryId = null)
            {
                string message = "[ eventCupId: " + eventCupId.ToString() + ", disciplineId:" + disciplineId + ", ownerTypeId: " + ownerTypeId + ", categoryId: " + categoryId + ", ageCategoryId " + ageCategoryId + " ]";
    
                return Ok(message);
            }
    


    Besides, I suggest you can pass a object which contains these fields. You can check the fields in Web API Function.

    The following links for your reference.

    How to pass json POST data to Web API method as an object:
    https://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-an-object

    Sending C# object to webapi controller
    https://stackoverflow.com/questions/19610883/sending-c-sharp-object-to-webapi-controller

    It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread.

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 8:26 AM