Several methods in the same ApiController

1

I'm creating an Api web project and wanted to put a set of methods on the same ControllerApi.

But in methods, I pass a Json object as a parameter. And since their structure is similar (see example), the controller is confused because it thinks they are all the same and so always calls the first method.

How can I resolve this?

Thank you in advance

Example:

public class MeuController : ApiCOntroller
{
    public List<Series> ObterSeries (ParametroSeries parametros)
    { ... }

    public List<Contas> ObterContas (ParametroContas parametros)
    { ... }
}

The call is being made as follows:

$.post("api/MeuController/ObterContas", { Identificacao: '12.234.567/0001-89' })
       .done(function (data) {  ...  })
});

$.post("api/MeuController/ObterSeries", { Identificacao: '12.234.567/0001-89' })
       .done(function (data) {  ...  })
});

Obs: Used json because there are other parameters that may or may not be informed

    
asked by anonymous 27.10.2014 / 15:07

1 answer

2

I would say the problem is even your routes.

As the first route that satisfies the request is used, and in your case the default route that is used by REST has been mapped first it may be being used, whereas its API follows the RPC style.

I would say that there are two alternatives in your case, first if you use only RPC you can remove the default route, this will remove the support for REST and everything else should continue as is.

Now if you want to use REST and RPC in the same project then you need to change one of the routes, for example changing routeTemplate from MeuApi to rpc/{controller}/{action}/{parametros} and then make the same modification in the call by javascript, changing the api by rpc in the address.

It may also be that simply changing the order in which the routes are registered solves the problem, but I can not say for sure if it would not cause a problem in the case of an API using REST.

    
27.10.2014 / 19:02