4

Tried both Table Controller and Custom Controller but not able to define two functions accepting the same parameters with same http method. For example when declaring

public Person GetMemberDetails(int id)
{
   // Some Code
   return person;
}

public Person GetMemberAddress(int id)
{
   // Some Code
   return person;
}

as both functions are requesting using GET and both have same input after building the project i am not able to use either of them. When i delete one or modifies one to use any other requesting method i am able to request from.

http://<azure-mobile-service-name>/Person/{id}

Is there any way to declare two functions with same signature and same method of request?

Tushar Vasudev
  • 104
  • 1
  • 8

3 Answers3

5

I've spent hours trying to get multiple post methods in Azure App Service (please notice that App Services replaced Mobile Services, ref: Upgrade your existing .NET Azure Mobile Service to App Service).

The general solution can be found in the aforementioned Multiple HttpPost method in Web API controller. However in case of App Services there's one very important comment. In the official Microsoft sample (ref: Work with the .NET backend server SDK for Azure Mobile Apps) the default config is proposed as:

HttpConfiguration config = new HttpConfiguration();

new MobileAppConfiguration()
    .UseDefaultConfiguration()
    .ApplyTo(config);

Unfortunately UseDefaultConfiguration() method calls MapApiControllers(), which defines standard routing "api/{controller}/{id}" with no constraints on {id}. Such routing is incompatible with "api/{controller}/{action}". So, if someone wants to use multiple post methods, standard configuration should be replaced with:

HttpConfiguration config = new HttpConfiguration();

new MobileAppConfiguration()
    .AddTables(new MobileAppTableConfiguration().MapTableControllers().AddEntityFramework()).AddMobileAppHomeController().AddPushNotifications()
    .ApplyTo(config);
config.Routes.MapHttpRoute("ActionApi", "api/{controller}/{action}");

Of course it is possible to use "api/{controller}/{action}/{id}" route instead, also with optional {id}.

I hope that my investigation can save someone many hours of nerves. If someone from Microsoft reads this post - please make a slight comment in the default sample or, better, add a parameter to UseDefaultConfiguration to decide whether to use "api/{controller}/{action}" routing.

Community
  • 1
  • 1
Dariusz Wasacz
  • 991
  • 1
  • 12
  • 16
  • 1
    Thank you! I have been trying to get this to work for 3-4 hours with a lot of googling. Your post solved my issue. One note though: Shouldn't it be MapTableControllers for MobileAppTableconfiguration and not MapApiControllers? – Stefan Wexel May 10 '17 at 12:15
  • Yes Stefan, you are right. I've corrected code in my post. Thank you. – Dariusz Wasacz May 11 '17 at 07:11
  • Don't forget to use: config.MapHttpAttributeRoutes(); otherwise the attributes will just be ignored... – Inna Dec 11 '17 at 11:18
4

You need to use the Route attribute, eg:

 [Route("api/getdetails")]
public Person GetMemberDetails(int id)
{
   // Some Code
   return person;
}
[Route("api/getaddress")]
public Person GetMemberAddress(int id)
{
   // Some Code
   return person;
}

Or search for "attribute routing" if you want the id in the route

Sentinel
  • 3,582
  • 1
  • 30
  • 44
  • Note also that you need to use the Mobile Services APIController rather than TableController, since TableController sets up some routes for you. – lindydonna Jul 22 '15 at 00:06
  • please is there a special way of calling these custom routes in the Mobile client ? – Damien Doumer Mar 25 '18 at 13:04
1

As per RESTful principles you can have only one method for a verb with one particular signature. But you can always modify your routing and achieve it but you won't be sticking to REST though. In some cases if the situation demands it is alright to do so. refer this post Multiple HttpPost method in Web API controller

Community
  • 1
  • 1
Aravind
  • 4,125
  • 1
  • 28
  • 39
  • Can you link to some authoritative document that confirms that REST does not permit multiple GETs on a controller? The OP's problem is that a single HTTP GET (route) is ambiguous, so Web API is in an error condition. – Sentinel Jul 20 '15 at 21:40
  • pls refer these.http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html https://github.com/flask-restful/flask-restful/issues/114 http://salesforce.stackexchange.com/questions/5693/can-we-create-multiple-http-methods-rest-annotations-of-the-same-type-in-a-si – Aravind Jul 29 '15 at 11:47