Webapi 2 Route Configuration web form application

Asked

Viewed 2,400 times

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.

  • 1

    Why you don’t use attributes by routes?

  • and how would that be? I’ve tried a lot...

  • This the way it is is not going to work. What is RouteTable?

  • @Alexbecker This is supposed to be a Webforms application?

  • No need to configure routes in Global.asax? I can decorate the parameter directly in the controller as I commented in the code above?

1 answer

2


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

I know you’ve done most of the work below, but just in case I’ll start from the beginning.:

Create a class WebApiConfig inside 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 }
        );
    }
}

In the global.asax only register the routes:

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

  • Very good that I need thank you, tried so much thing that had already disorganized my code... thanks

  • @Alexbecker, be sure to access the link I put at the end of the reply, maybe it will help you organize your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.