Error 404 when trying to make a httpget to an Action of a webapi controller

3

I add a controller webapi , the IDE is responsible for generating the class WebApiConfig and registering the configurations in the global.asax , until here everything is perfect. I run my application

asked by Alan 29.01.2016 в 20:08
source

3 answers

1

I found the problem, it is the order of registration of the routing configurations. The IDE automatically adds everything to the end (see my question), this causes the mentioned malfunction.

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    //Registrar aquí las configuraciones webapi
    GlobalConfiguration.Configure(WebApiConfig.Register);

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);


    //no registrar aquí las configuraciones webapi
}
    
answered by 01.02.2016 / 16:06
source
1

Remember that Get() must invoke it using the correct http verb, you will only enter it if you used a GET of the http.

Using the GET as a verb and the url http://{sitio}/api/nombreController should work

You can use postman or fiddler to try the webapi.

Also try to define the attributes

[Route("api/Gerencias")]
[HttpGet]
public IEnumerable<Gerencia> Get(){

}

I mention this because of what I saw in this article

Attribute Routing in ASP.NET Web API 2

Another way could be

[Route("")]
public IEnumerable<Gerencia> Get(){

}

Analyze in the article the title "Route Prefixes" there mention this comment. It is more there in the controller defines the attribute [RoutePrefix("api/books")]

    
answered by 29.01.2016 в 20:21
0

Try it by adding the action tag in the routerTemplate property, for example:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
    
answered by 30.01.2016 в 16:27