Search code examples
.net-coreasp.net-core-webapi

.net core web API. How should my route allow for optional parameters when the last one just needs to be set


I've got the following request from my employer and I was wondering if you could give me a suggestion.

For simplicity here is an example of what I have for the following get student controller

I've created a GET route with the following parameters api/students/{LcID}/{AffiliateId}/{StartDate?}/{EndDate?}/{Email?}

The StartDate, EndDate, and Email are optional parameters. Everything works fine with this setup but my employer wants to know if the client can just send the LcId, AffiliateId, and Email. Can that be done?

I've tried using Postman to set the name and value parameters LcID=1/AffiliateId=123/[email protected] but that didn't work.

Should I change my parameters into an object that needs to be passed in? I don't really want the user to have to fill in the other fields with dummy values in-between the parameters that do get filled in. I hope this makes sense.


Solution

  • You can add another Route to your Action, like the following:

     [HttpGet("LcID={LcID}/AffiliateId={AffiliateId}/{StartDate?}/{EndDate?}/{Email?}")]
     [HttpGet("LcID={LcID}/AffiliateId={AffiliateId}/Email={Email}")]
    

    Then in your Postman: enter image description here

    If there are other requirements, you can continue to add them.