请原谅我在这方面的无知。我已经阅读了许多线程,但仍然无法正确路由。

我有一个这样的ProductsController:

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;
    }
}

我有一个控制台测试程序,在其中定义了路由:
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/products/GetListOfStudents",
defaults: new { controller = "products", action = "GetListOfStudents" });

但是当我打电话时
GET http://localhost:8080/api/Products/GetListOfStudents

我收到错误消息:
MessageDetail=No action was found on the controller 'Products' that matches the name 'GetListOfStudents'.

我一直在拔头发,无法确定正确的路线。

有谁愿意帮助我?

最佳答案

好的,谢谢您的帮助!

我为使其正常工作所做的事情:

  • 从GetListOfStudents函数中删除了“静态”。
  • 添加了以下路线。

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

    谢谢大家的帮助!

    关于c# - 在 Controller 上找不到与请求匹配的操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22914928/

    10-12 06:31