Reputation: 4425
I have two methods in my api controller with same name and same number of parameters but the type of one of the parameter is different. You can see here
[HttpGet]
public dynamic Add(String organizationId, Driving driving)
[HttpGet]
public dynamic Add(String organizationId, bool driving)
I am trying to call the api like this
var data {organizationId: "something", driving: true };
var ajaxConfig = {
url: some url,
type: "GET",
dataType: 'json',
crossDomain: true,
success: function (data) {
onDone();
callback(data);
},
error: function (error, textStatus, errorThrown) {
}
};
ajaxConfig.data = data;
$.ajax(ajaxConfig);
The system gets confused between which api to call. Am I doing it wrong? Is there some other way to do it?
Upvotes: 1
Views: 8299
Reputation: 349
The controller can only make the difference between two identically named methods in two ways:
In case you dont want to make the distinction in the client and you want your server to do the distinction you need another approach.
You can create a method like this:
[HttpGet]
public dynamic Add(String organizationId, object driving)
{
if (driving is Driving)
// execute code
else
// execute other code
}
This example is far from complete but it could be an approach.
With regards, John
Upvotes: 1
Reputation: 16846
You could use attribute routing to make the methods point to different routes in the controller
[HttpGet]
[Route("api/controller/add1")] // Url http://url.domain.com/api/controller/add1
public dynamic Add(String organizationId, Driving driving)
[HttpGet]
[Route("api/controller/add2")] // Url http://url.domain.com/api/controller/add2
public dynamic Add(String organizationId, bool driving)
Upvotes: 3