WebAPI 2 Routing configuration web form application

1

For about 6 months I've been entering the MVC world, before I've used a lot of WebForms and I confess that I'm having trouble understanding how to configure the routes for a ApiController , so I noticed I can not have two equal queries (with the same parameters), but I'm a bit clumsy to set this up.

Below I will put an example code that is giving the error below when 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'."
}

GraphicsController

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 Global.asax I'm doing 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 use the same controller for more than one function. Is that it?

I have already researched several sites, but I have not found any that have an explanation that I understand.

    
asked by anonymous 04.07.2016 / 15:59

1 answer

2

Alex, first that you do not need to log multiple routes in your Global.asax , s you need to customize a route using the Route attribute.

I know you've done a lot of the work below, but just for the sake of completeness I'll start at the beginning.

Create a class WebApiConfig within App_Start :

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

No global.asax perform only route logging:

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);          
}

Now in your classes, do so:

[RoutePrefix("api/graficos")]
public class GraficosController : ApiController
{

    [HttpGet]
    [Route("BuscaUsuarioDashboard/{param}")]
    public List<UsuarioDashboard> BuscaUsuarioDashboard(string param)
    {
        return Fachada.FachadaEstatisticas.BuscaUsuarioDashboard(param);
    }

    [HttpGet]
    [Route("EmailsNaoEnviados/{data}")]
    public List<EmailNaoEnviado> EmailsNaoEnviados(string data)
    {
        return Fachada.FachadaEstatisticas.EmailsNaoEnviados(Convert.ToDateTime(data));
    }
}

Now you can make the following calls:

GET: api/graficos/EmailsNaoEnviados/2016-01-01
GET: api/graficos/BuscaUsuarioDashboard/userName

SOURCE: Attribute Routing in ASP.NET Web API 2

    
04.07.2016 / 16:18