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
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
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
}
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")]
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 }
);