I am working on an ASP.Net Core web API using REST verbs with optional query string parameters.
For example, the API call looks like this;
http://localhost:5010/servicetypecode?stc=123
or http://localhost:5010/servicetypecode?mailclasscode=fc
I am using code like this to set up the query strings
public IActionResult Get([FromQuery] string mailclasscode, [FromQuery] string stc) { ... }
So, in my method body, I am evaluating each string individually like this...
if (!string.IsNullOrEmpty(mailclasscode)) { ... }
if (!string.IsNullOrEmpty(stc)) { ... }
However, I would like to allow my users to just make a straight up GET to the API with no query string parameters provided and return a list of all records, unfiltered.
So a call like this;
http://localhost:5010/servicetypecode
But I would prefer not to have to do this in my method body, especially if I were to have a lot of query string parameters;
if (string.IsNullOrEmpty(mailclasscode) && string.IsNullOrEmpty(stc)) { ... }
Is there a way to simply determine if no query string parameters were provided without evaluating every possible query string parameter?
Also, is there a better way to evaluate query string parameters than testing every possible parameter, in the case where I were to pass a lot of different query string parameters?
Thanks in advance for any help you can provide.
You can check the incoming request.
if (this.HttpContext.Request.QueryString.HasValue)
Also as a note, if you have a lot of query string parameters, you can use a single class instead of multiple parameters in the method signature.