Is there a Web API equivalent to the MVC ActionMethodSelectorAttribute?
My specific purpose is this: I have, for example, a ResourceController and when I POST to the controller, I’d like to be able to receive a single resource (Resource) or a list (IEnumerable<Resource>).
I was hoping creating two methods with different parameters would cause the deserialization process to do some evaluation but this doesn’t seem to be the case (and frankly, I don’t think it’s efficiently realistic with the combination of content negotiation and the fact that many data formats, like JSON, make it difficult to infer the data type). So I originally had:
public HttpResponseMessage Post(Resource resource) {...}
public HttpResponseMessage Post(IEnumerable<Resource> resources) {...}
…but this gets the “multiple actions” error. So I investigated how to annotate my methods and came across ActionMethodSelectorAttribute but also discovered this is only for MVC routing and not Web API.
So… without requiring a different path for POSTing multiple resources vs. one (which isn’t the end of the world), what would I do to differentiate?
My thoughts along the ActionMethodSelectorAttribute were to require a query parameter specifying multiple, which I suppose is no different than a different path. So, I think I just eliminated my current need to do this, but I would still like to know if there is an equivalent ActionMethodSelectorAttribute for Web API 🙂
I haven’t seen a replacement for that method (there is an
IActionMethodSelectorinterface but it is internal to the DLL). One option (although it seems like it might be overdoing it) is to overload theIHttpActionSelectorimplementation that is used.But changing gears slightly, why not always expect an
IEnumerable<Resource>? My first guess is that the collection method (that takesIEnumerable<Resource>) would simply loop and call the single value (justResource) function?