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

I tried both Table Controller and Custom Controller, but could not determine two functions that take the same parameters with the 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;
}

since both functions request the use of GET, and both have the same input after creating the project, I cannot use either of them. When I delete one or modify it to use any other request method that I can request.

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

Is there a way to declare two functions with the same signature and the same request method?

+4
source share
3 answers

You need to use the Route attribute, for example:

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

" ", ,

+4

, Azure App Service ( , App Services Mobile Services, ref: .NET Azure Mobile Service ).

HttpPost Web API. App Services . Microsoft (: SDK backend .NET Azure Mobile Apps). :

HttpConfiguration config = new HttpConfiguration();

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

, UseDefaultConfiguration() MapApiControllers(), "api/{controller}/{id}" {id}. "api/{controller}/{action}". , - post, :

HttpConfiguration config = new HttpConfiguration();

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

, "api/{controller}/{action}/{id}", {id}.

, - . - Microsoft , , , , UseDefaultConfiguration, , api/{controller}/{action}.

+4

According to RESTful principles, you can have only one method for a verb with one specific signature. But you can always change your routing and achieve it, but you will not stick to REST. In some cases, if the situation requires that everything is in order. link to this post A few HttpPost methods in the web API controller

+1
source

Source: https://habr.com/ru/post/1598609/


All Articles