Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No action was found on the controller that matches the request

Please excuse my ignorance in this area. I have read many threads and still cannot get my routing correct.

I have a ProductsController like this:

public class ProductsController : ApiController
{
    [ActionName("GetListOfStudents")]
    public static List<Structures.StudentInfo> GetListOfStudents(string Username, string Password)
    {
        List<Structures.StudentInfo> si = StudentFunctions.GetListOfStudents(Username, Password);
        return si;
    }
}

I have a console test program where I have defined the route:

config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/products/GetListOfStudents",
defaults: new { controller = "products", action = "GetListOfStudents" });

But when I run call

GET http://localhost:8080/api/Products/GetListOfStudents

I get the error message:

MessageDetail=No action was found on the controller 'Products' that matches the name 'GetListOfStudents'.

I have been pulling my hair out and cannot work out what the correct route should be.

Would any kind person care to help me out?

like image 404
Trevor Daniel Avatar asked Apr 07 '14 14:04

Trevor Daniel


2 Answers

Ok- thanks for the help peeps!

This what I did to get it working:

  1. Removed the "static" from the GetListOfStudents function.
  2. Added the route below.
config.Routes.MapHttpRoute(
  name: "ApiByAction",
  routeTemplate: "api/products/GetListOfStudents/{username}/{password}",
  defaults: new { controller = "products", action = "GetListOfStudents" }
);

Thanks everyone for your help!

like image 164
Trevor Daniel Avatar answered Sep 22 '22 18:09

Trevor Daniel


When registering your global api access point, you should tell the config which route to use in the following manner:

config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}
defaults: new { controller = "products", action = "GetListOfStudents" });

In this sample you explicitly tell the controller it should only go to the "products" controller, you can make it generic without specifying the control or the action, just omit the defaults, like this:

config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}

That should do the job :)

like image 32
Yuval Itzchakov Avatar answered Sep 22 '22 18:09

Yuval Itzchakov