1
About 6 months ago I am entering the MVC world, before I used a lot of Webforms and I confess that I have difficulties understanding how to configure the routes for a ApiController
, from what I noticed I can’t have two identical queries (with the same parameters), but I’m a little embarrassed to set this up.
Below I will put an example of code that is giving the error below while trying to access them.
{
"Message":"No HTTP resource was found that matches the request URI 'http://localhost:54596/api/graficos/usuarios/'.",
"MessageDetail":"No action was found on the controller 'Graficos' that matches the name 'usuarios'."
}
Graphiccontroller
public class GraficosController : ApiController
{
[HttpGet]
//[Route("api/graficos/usuarios/{action}/{param}")]
public List<UsuarioDashboard> BuscaUsuarioDashboard(string param)
{
return Fachada.FachadaEstatisticas.BuscaUsuarioDashboard(param);
}
[HttpGet]
//[Route("api/graficos/emails/{action}/{param}")]
public List<EmailNaoEnviado> EmailsNaoEnviados(string data)
{
return Fachada.FachadaEstatisticas.EmailsNaoEnviados(Convert.ToDateTime(data));
}
}
In the Global.asax I’m making the route as follows:
RouteTable.Routes.MapHttpRoute(
name: "Site",
routeTemplate: "api/{Controller}/{action}/{site}",
defaults: new { }
);
RouteTable.Routes.MapHttpRoute(
name: "Usuarios",
routeTemplate: "api/{controller}/{action}/{param}",
defaults: new { }
);
RouteTable.Routes.MapHttpRoute(
name: "Graficos",
routeTemplate: "api/{controller}/{action}",
defaults: new { }
);
I think my mistake is trying to take advantage of the same controller for more than one function. That’s it?
I’ve searched a lot of sites, but I can’t find one that has an explanation that I understand.
Why you don’t use attributes by routes?
– Jéf Bueno
and how would that be? I’ve tried a lot...
– Alex Becker
This the way it is is not going to work. What is
RouteTable
?– Leonel Sanches da Silva
@Alexbecker This is supposed to be a Webforms application?
– Jéf Bueno
No need to configure routes in Global.asax? I can decorate the parameter directly in the controller as I commented in the code above?
– Alex Becker