1

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?

1
  • I would keep the routes different. But you can try giving route constraints '[Route("Add/{organizationId}/{driving:bool}")]' for the second method. Not sure whether this works, I've never tried Commented Apr 21, 2017 at 7:00

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly my thoughts.
But then while calling them from client would the url be "somthing/Add" or "something/method1 etc?"
@ChaudhryMohsinAli updated the question. From client you call it "api/controller/add1"
1

The controller can only make the difference between two identically named methods in two ways:

  • Via HttpVerb attributes: In this case you could apply [HttpGet] to the first method and [HttpPost] to the second. When calling from a client you must use the Get method when using the first one and the Post method when using the second one.
  • Via Route attribute: In this case you tell MVC what route to take for each method (ex: [Route("api/mycontroller/AddWithBoolean")] ). In this case the client chooses the correct method by using the correct route.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.