0

In ASP.NET Web API, how can I get two actions, both routed for the same URL and same HTTP method, to be distingushable only by their different parameter types?

I'd like to be able to POST either a single JSON object or a JSON array of objects.

I'm expecting it to work like this:

[HttpPost]
public virtual object Post([FromBody]IDictionary<string, object> values)
{
    //add new object to collection
}

[HttpPost]
public virtual IEnumerable<object> Post([FromBody] IDictionary<string, object>[] array)
{
    return array.Select(Post);
}

However, if I do this and attempt to call either actions, I get this error:

Multiple actions were found that match the request
Connell
  • 13,925
  • 11
  • 59
  • 92
  • May be this could be a help: http://stackoverflow.com/questions/11407267/multiple-httppost-method-in-web-api-controller – Habib Jan 31 '14 at 15:09

1 Answers1

0

Have a look at the attribute routing library. This allows you to define routes for each action in your web api. This has even been packaged as part of Web Api 2.0:

[POST("First URL here")]
public virtual object Post([FromBody]IDictionary<string, object> values)
{
    //add new object to collection
}

[POST("Second URL here")]
public virtual IEnumerable<object> Post([FromBody] IDictionary<string, object>[] array)
{
    return array.Select(Post);
}

This way you will be able to manually define your routes for each method.

James
  • 2,195
  • 1
  • 19
  • 22
  • Thanks for your answer. I forgot to mention I'd like them to be the same URL too (updated the question). Otherwise I could just set up another route in the `WebApiConfig.cs`. – Connell Jan 31 '14 at 16:00
  • Note that attribute routing is built into Web Api 2 itself – Kiran Jan 31 '14 at 16:57