Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI No action was found on the controller

I got an error - No action was found on the controller 'Action' that matches the request.

The url is http://localhost:37331/api/action/FindByModule/1.

The routing I used is

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

Controller:

public class ActionController : ApiController {     private IActionRepository repository = null;      [HttpGet]     [ActionName("All")]     public IEnumerable<JsonAction> All()     {         return from action in this.repository.Get()                select new JsonAction                {                    ID = action.ID,                    Text = action.Text.Trim(),                    Description = action.Description.Trim(),                };     }      [HttpGet]     [ActionName("FindByModule")]     public IEnumerable<JsonAction> FindByModule(Int64 moduleId)     {         return from action in this.repository.FindByModule(moduleId)                select new JsonAction                {                    ID = action.ID,                    Text = action.Text.Trim(),                    Description = action.Description.Trim(),                };     } } 
like image 870
Botem Bao Avatar asked Jan 23 '13 14:01

Botem Bao


People also ask

How can I call MVC controller action from Web API?

You just need to make sure that you get your URLs right, so WebApi calls your MVC controller via it's properly qualified route. To test this, write a simple MVC action which returns some Json data and call it in the browser. If you manage to craft the URL correctly you will see the data displayed in the browser.

What is Web API action?

This article explains actions in the ASP.NET Web API. Action methods returns the response of the user input and it also returns an action result. ActionResult. ActionResult is a class, all the action methods depend on the ActionResult class, so it is called the base of the action method.


2 Answers

This is because there is a parameter name mismatch. From your route the value 1 is assigned to parameter named id and your action is looking for parameter named moduleId.

First option is to change your route like this:

config.Routes.MapHttpRoute(     name: "DefaultApi",     routeTemplate: "api/{controller}/{action}/{moduleId}",     defaults: new { moduleId = RouteParameter.Optional } ); 

Second is to change your URL like this:

http://localhost:37331/api/action/FindByModule?moduleId=1 

So the parameter name match.

like image 51
tpeczek Avatar answered Sep 23 '22 05:09

tpeczek


My api had too many parameters and I was getting an error. I solved the problem with Route.

[Route("addressverification/{id}/{no}/{day}/{month}/{year}")] public AdressVerificationResult  Get(long? id, long? no ,long? day, long? month, long? year)         {                  return new AdressVerificationResult               {                 Aciklama = "19........4 kimlik numaralı kişinin 18.......1 adres numarasında 'YerlesimYeri' adres tipi için geçerli bir yurtiçi adres beyanı mevcuttur.",                 DurumKod = true             };            }          
like image 41
BurhanY Avatar answered Sep 20 '22 05:09

BurhanY