0

I am developing a RESTful web service by WebAPI, I need to overload one of my methods, for example:

[HttpGet]
public void Send(string body, string phoneNumber)

and

 [HttpGet]
  public void Send(string body, [FromUri] List<string> phoneNumber)

When I call this service by using this address :

http://webservice.com/api/send?body=Hello&phoneNumber=1345456,123123

the first method is called but I prefer to call the second method by using this address:

http://webservice.com/api/send?body=Hello&phoneNumber=134556&phoneNumber=123123

but the first method is called again!

Is it possible to have overload for methods in WebAPI?

Pooya Yazdani
  • 1,141
  • 3
  • 15
  • 25

2 Answers2

2

You can't do this.

You can't overload (based on a parameters type) a wepapi action methods having the same route.

You need to do somethig like this:

public void Send(string body, [FromUri] List<string> phoneNumber)
{
    if (string.IsNullOrWhiteSpace(body) || phoneNumber == null)
    {
        // return error message
    }

    if (phoneNumber.Count == 1 && phoneNumber[0].IndexOf(',') > -1)
    {
        var phonenumbers = phoneNumber[0]
        .Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
        // execute method#1 and pass array of phonenumbers
    }
    else
    {
        // execute method#2 and pass list of phonenumbers
    }
}
Yuriy A.
  • 750
  • 3
  • 19
1

One possible solution would be to use the ActionName attribute. An example:

[HttpGet]
[ActionName("SendWithPhoneNumber")
public void Send(string body, string phoneNumber)

[HttpGet]
[ActionName("SendWithPhoneNumbers")
public void Send(string body, [FromUri] List<string> phoneNumber)

You can also use attribute routing if you like. For more information have a look at this thread.

Community
  • 1
  • 1
Horizon_Net
  • 5,959
  • 4
  • 31
  • 34