2

I have an Asp.Net Web API with the following function

[HttpPost]
[Route("Employees")]
public IHttpActionResult Employees(SearchBindingModel searchOptions)
{
   ...
}

It's a function to search employees, so it should be a HttpGet instead of HttpPost. The problem is that the SearchBindingModel is really complex and I don't know how I could write it as a query string.

The class look like this

public class SearchBindingModel
{
    public IList<Criteria> Criterias { get; set; }
}


public class Criteria
{
   ...
}



public class FooCriteria : Criteria
{
   ...
}



public class BarCriteria : Criteria
{
   ... 
}

Since a query string cannot contain hierarchy should I rethink my Web API?

or should I keep using HttpPost instead?

Marc
  • 16,170
  • 20
  • 76
  • 119
  • 1
    You can bind to your model using `...?Criterias[0].someProperty=someValue&Criterias[0].anotherProperty=anotherValue&Criterias[1].someProperty=someValue&.....` but this will generate a really ugly query string –  Nov 10 '15 at 23:33
  • 1
    @Marc why not ?When you have a complex search param why you must have a complex query string?!!! I think your approach is right. – M.Azad Nov 11 '15 at 08:50
  • Downvoter... could you explain? – Marc Nov 11 '15 at 12:33

1 Answers1

2

In short, follow best practise where it is possible but don't get hung up on it. It seems like you have a valid use case for using a POST. Being 'truly RESTful' is mostly down to interpretation of several competing standard (http client and server which are ever so slightly different etc).

For complex large models it is sometimes just not practical to use a GET, depending on the consumer of your API (via a browser or not) you may want to take into considerations browser URL length limits for the query string as described over https://stackoverflow.com/a/417184/1422377 This is particularly important if you will be passing in lists of a complex model.

Alternatively, if you really must use GET, WEB API will accept the model from the Body like the sample below. However, I would advise caution with this as certain clients will not be able to send a body to a get request (for example if your client was .net they would have to use the RestSharp lib instead of the native libraries).

public IHttpActionResult Employees([FromBody]SearchBindingModel searchOptions)
Community
  • 1
  • 1
BMac
  • 2,183
  • 3
  • 22
  • 30
  • Will it confuse people if I use httppost or it's common? – Marc Nov 11 '15 at 12:50
  • 1
    Hi @Marc it is fairly common, more so than passing a body for a GET request, in my experience. Really you want to do what will be easiest for your API user (i.e. the other developer), I always promote the "Eat your own dogfood" approach when designing an API - put together a sample app yourself and see how it feels, its a great way of eliminating bugs. Alternatively clear documentation (currently using SWAGGER via swashbuckle myself) helps in the ambiguity as to why you chose one way of the other. – BMac Nov 11 '15 at 13:21