1

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 :)

4

1 回答 1

1

我还没有看到该方法的替代品(有一个IActionMethodSelector接口,但它在 DLL 内部)。一种选择(尽管看起来可能有些过头了)是重载所IHttpActionSelector使用的实现。

但是稍微改变一下,为什么不总是期待一个IEnumerable<Resource>?我的第一个猜测是收集方法(需要IEnumerable<Resource>)会简单地循环并调用单个值(只是Resource)函数吗?

于 2012-08-17T11:35:30.780 回答