Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom api routes in Azure Mobile Service when using .NET as Back end?

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?

like image 661
Tushar Vasudev Avatar asked Dec 25 '22 16:12

Tushar Vasudev


2 Answers

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.

like image 108
Dariusz Wasacz Avatar answered Jan 26 '23 00:01

Dariusz Wasacz


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

like image 45
Sentinel Avatar answered Jan 25 '23 23:01

Sentinel