1

I've got an MVC solution that exposes data from a database.

They issue an HttpGet and my controller provides the data to the client:

[HttpGet]
[Route("{someGuid:guid}")]
public HttpResponseMessage Get([FromUri] Guid? someGuid)
{
    var responseData = _someService.RecordSet.Where(x=>x.guid == someGuid);

    return Request.ResponseMessageFromApiEntity(responseData);
}

How would I implement a way for the client to be able to issue a request with multiple combinations of parameters? They could pass in 2 or 5 or up to 10, and I would need to filter the dataset by those parameters.

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • I have multiple get endpoints. The controlers are smart enought to figure out which one is being used. Something like this: www.YourURL/api/controller?var1=value&var1=value public IHttpActionResult Get(string var1, string var2) – Jared Stroebele Aug 31 '16 at 19:26
  • what if they decide to just pass in Honda? – Alex Gordon Aug 31 '16 at 19:28
  • try this article. http://www.tugberkugurlu.com/archive/asp-net-web-api-catch-all-route-parameter-binding. It just sets a deliminator to split the parameters on. I am using mostly web api so I would just stick an object into the http request body. – Jared Stroebele Aug 31 '16 at 19:32
  • You mean pass multiple guids? Just declare it as an array as in [this answer](http://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-asp-net-web-api). Or do you mean different parameter names? Just add them as nullable parms to the method and use `Where` chaining. – stephen.vakil Aug 31 '16 at 19:38
  • @stephen.vakil i need multiple parameters, what if the client passes in an empty parameter, how would my Where work? how would i chain the where's in a conditional fashion (conditional based upon whether a parameter was provided or not) – Alex Gordon Aug 31 '16 at 19:47

1 Answers1

0

Take advantage of the Pipes and Filters Pattern

[HttpGet]
[Route("{someGuid:guid}")]
public HttpResponseMessage Get([FromUri] Guid? someGuid, [FromUri] int someInt)
{
    var responseData = _someService.RecordSet.AsQueryable(); // or some base query or equivalent
    if (someGuid.HasValue)
    {
       responseData = responseData.Where(x=>x.guid == someGuid);
    }

    if (someInt.HasValue)
    {
       responseData = responseData.Where(x=>x.int == someInt);
    }

    return Request.ResponseMessageFromApiEntity(responseData);
}
Community
  • 1
  • 1
stephen.vakil
  • 3,492
  • 1
  • 18
  • 23