0

I have an Api controller with the following method:

[Route("{app}")]
public IHttpActionResult Put(string app, Property setting){ //do stuff }

I want to over load it with:

[Route("{app}")]
public IHttpActionResult Put(string app, Property[] settings){ 
    foreach (Property property in settings)
    {
        Put(app, property);
    }
    return Ok("Settings Updated");
}

This overload causes a routing conflict. I don't want to change the route. How can I achieve what I want?

  • 3
    Possible duplicate of [Overload web api action method based on parameter type](http://stackoverflow.com/questions/14353466/overload-web-api-action-method-based-on-parameter-type) – Igor Aug 19 '16 at 15:26

1 Answers1

0

You're using attribute routing here with two put requests. Firstly I'd suggest to use a [FromBody] atrribute before the Property[] settings parameter here.

Based on the way you used Route Attributes here points to the same action where you have multiple assigned already. So I can suggest two things here.

  1. http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection.
  2. Just use the second action that you mentioned that takes Property[] settings and just check whether the array is empty or not and you really don't have to worry about one setting change or more as essentially it's the same underneath. :)

And as you're using Asp.net Web Api 2 this could contain important information.

Community
  • 1
  • 1
Swagata Prateek
  • 1,076
  • 7
  • 15
  • Thank you for your suggestions. About your second suggestion. It's best to keep both puts because people already wrote javascript for the Put that works on only one setting. – haodong tao Aug 19 '16 at 15:52
  • Then you can definitely go ahead defining a route for yourself that would allow you to do what you want. :) The basics are defined [here](http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api) and you can also have a look [here](http://stackoverflow.com/questions/11407267/multiple-httppost-method-in-web-api-controller) – Swagata Prateek Aug 19 '16 at 20:49