0

In my web api controller i have two functions with following structure

public HttpResponseMessage Post(CountryDto country)
 {
    var countries = _countryAppService.RegisterNewCountry(country);
    var message = Request.CreateResponse(HttpStatusCode.Created, countries);
    return message;
 }

public HttpResponseMessage Post(int countryId, StateDto state)
{
    var country = _countryAppService.AddNewState(state, countryId);
    var message = Request.CreateResponse(HttpStatusCode.Created, country);
    return message;
}

I need to call the second overloaded version of post , i tried this using fiddler with following http request details

POST http://localhost:51830/api/Country/ HTTP/1.1
User-Agent: Fiddler
Host: localhost:51830
Content-Type: application/json; charset=utf-8
Content-Length: 63

{"countryId":5,"state":{"StateName":"Dallas","StateCode":"DA"}}

but its calling the first overloaded post instead of second post, what i am missing and how i can call the second post using fiddler

Binson Eldhose
  • 993
  • 3
  • 14
  • 35
  • Think that this problem has to do with the model binder (but not sure). A workaround would be to use different routes, for example with the [attribute routing](http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2) or with the [ActionName](http://stackoverflow.com/questions/11407267/multiple-httppost-method-in-web-api-controller) attribute. – Horizon_Net Jul 04 '14 at 11:00

2 Answers2

0

You need to specify the [FromBody] attribute on the StateDto parameter in the 2nd method.

http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

demoncodemonkey
  • 11,730
  • 10
  • 61
  • 103
0

There's no support for overloading in WebAPI (nor in MVC).

However you can use attribute-based routing to achieve what you want, i.e.:

[Route("api/country/add")]
public HttpResponseMessage AddCountry(CountryDto country)

[Route("api/country/addstate")]
public HttpResponseMessage AddCountry(int countryId, StateDto state)

You need to make sure you call config.MapHttpAttributeRoutes(); before the rest of the routing in configuration(i.e.: config.Routes.MapHttpRoute...)

Rui
  • 4,847
  • 3
  • 29
  • 35