Asp.net API: no action found on controller

So it really caused me a headache.

Route:

config.routes.MapHttpRoute( name: 'ControllerAction' , routeTemplate: "api/{controller}/{action}" ); 

Controller:

 public class LookupsController : ApiControllerBase { [ActionName("all")] public Lookups GetLookups() { var lookups = new Lookups { Customers = GetCustomers().ToList(), //add more }; return lookups; } } 

but when I try to get into uri: / api / lookups / all I get 404 error:

"No actions were found on the Lookups controller that match the name all.

Any help would be appreciated

EDIT: So I finally understood. it was due to a wrong attitude. VS2012 automatically allowed an action for system.web.mvc.actionnameattribute, while I needed system.web.http.actionnameattribute.

strange problem anyway, i hope this helps someone else.

+4
source share
1 answer

Jacob Lee

EDIT:

You rename the controller, but do not specify a method. WebApi allows the method from the prefix GET, PUT, DELETE, POST. Therefore, the method attribute must be placed in the action signature:

Try this, works well for me:

<i>

 public class LookupsController : ApiControllerBase { [HttpGet()] [ActionName("All")] public Lookups GetLookups() { var lookups = new Lookups { Customers = GetCustomers().ToList(), //add more }; return lookups; } 

}

 routes.MapHttpRoute( name: "ActionApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } 

);

Take a look here for more information on routing:

Routing in the ASP.NET Web Interface

Hope this helps you!

+1
source

All Articles