在与请求匹配的控制器上没有发现任何操作

时间:2022-10-28 14:51:48

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:

我有这样一个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;
    }
}

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?

有善良的人愿意帮助我吗?

Thanks

谢谢

7 个解决方案

#1


11  

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

在注册全局api接入点时,应该以以下方式告诉配置要使用哪条路由:

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:

在这个示例中,您显式地告诉控制器它应该只指向“products”控制器,您可以使它成为通用的,而无需指定控件或操作,只需省略默认值,如下所示:

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

That should do the job :)

这应该可以做到:

#2


11  

Ok- thanks for the help peeps!

好的-谢谢你的帮助偷窥者!

This what I did to get it working:

这就是我让它工作的方法:

  1. Removed the "static" from the GetListOfStudents function.
  2. 从GetListOfStudents函数中删除“静态”。
  3. Added the route below.
  4. 添加下面的路线。
config.Routes.MapHttpRoute(
  name: "ApiByAction",
  routeTemplate: "api/products/GetListOfStudents/{username}/{password}",
  defaults: new { controller = "products", action = "GetListOfStudents" }
);

Thanks everyone for your help!

谢谢大家的帮助!

#3


6  

Your GetListOfStudents action requires two parameters, username and password. Yet, the route definition contains neither specification in the route template where the values for those parameters should come from, nor specification for those parameter defaults in the defaults: parameter definition.

GetListOfStudents操作需要两个参数:用户名和密码。然而,路由定义既不包含路由模板中应该包含这些参数的值的规范,也不包含默认参数的规范:参数定义。

So when request comes in, routing is able to find your controller, but it is unable to find the action that it can call with the request and route context that it has because it has no information for the username and password parameters.

所以当请求进来时,路由能够找到你的控制器,但是它不能找到它可以用请求和路由上下文调用的动作,因为它没有用户名和密码参数的信息。

#4


3  

The most important is: ASP.Net's mvc not only seek action by name, also it will check method's signature, only the method is non-static, name matches and parameters matches, the action will be executed.

最重要的是:ASP。Net的mvc不仅要通过名称来查找操作,还会检查方法的签名,只有方法是非静态的,名称匹配和参数匹配,操作将被执行。

for your case, there are two ways to correct it. one way is declare default value, mvc will use default value is no such parameters

对于你的情况,有两种方法可以纠正它。一种方法是声明默认值,mvc使用默认值是没有这样的参数的

public List<Structures.StudentInfo> GetListOfStudents(string Username = null, string Password = null)
{
    List<Structures.StudentInfo> si = StudentFunctions.GetListOfStudents(Username, Password);
    return si;
}

the second way is use override

第二种方法是使用override

public List<Structures.StudentInfo> GetListOfStudents()
{
   return GetListOfStudents(null, null);
}

public List<Structures.StudentInfo> GetListOfStudents(string Username, string Password)
{
    List<Structures.StudentInfo> si = StudentFunctions.GetListOfStudents(Username, Password);
    return si;
}

#5


0  

I had this problem and solved it by including the verb as part of the action (i.e. GetThis, GetThat) and manually creating routes. I was attempting to create routes using attributes, but that did not work. This SO question may be the answer as to why the attributes aren't working, I haven't gotten that straightened out yet. As an additional note for anyone else having the same problem, when debugging it locally, IE was crashing when the "no action found" xml was returned. Once I gave up and switched to Chrome, the message detail was returned, and it was obvious that my controller at least was being found, it was just a matter of getting the action to work...

我有这个问题,并通过将动词作为动作的一部分(即GetThis, GetThat)和手动创建路由来解决它。我试图使用属性创建路由,但这行不通。这个问题可能是为什么属性不工作的答案,我还没弄清楚。对于任何有同样问题的人来说,当在本地调试IE时,当返回“没有找到操作”xml时,IE会崩溃。一旦我放弃并切换到Chrome,消息细节被返回,很明显我的控制器至少被找到了,这仅仅是让动作开始工作的问题……

#6


0  

If you want to call GetListOfStudents method without parameter you must set default value for parameter. such as GetListOfStudents(string Username=null, string Password=null) Otherwise you must call method with Parameters. GET http://localhost:8080/api/Products/GetListOfStudents/Username/Password

如果要调用没有参数的GetListOfStudents方法,必须为参数设置默认值。例如GetListOfStudents(string Username=null, string Password=null),否则必须使用参数调用方法。http://localhost:8080 / api /产品/ GetListOfStudents /用户名/密码

#7


-2  

When sending, encode the password with base64. Then when you about to use it decode it.

发送时,用base64对密码进行编码。然后当你准备用它解码它。

byte[] numArray = Convert.FromBase64String(password);
string Pass = Encoding.UTF8.GetString(numArray); 

List<Structures.StudentInfo> si = StudentFunctions.GetListOfStudents(Username, Pass);

Works fine for me.

对我来说很不错。

#1


11  

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

在注册全局api接入点时,应该以以下方式告诉配置要使用哪条路由:

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:

在这个示例中,您显式地告诉控制器它应该只指向“products”控制器,您可以使它成为通用的,而无需指定控件或操作,只需省略默认值,如下所示:

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

That should do the job :)

这应该可以做到:

#2


11  

Ok- thanks for the help peeps!

好的-谢谢你的帮助偷窥者!

This what I did to get it working:

这就是我让它工作的方法:

  1. Removed the "static" from the GetListOfStudents function.
  2. 从GetListOfStudents函数中删除“静态”。
  3. Added the route below.
  4. 添加下面的路线。
config.Routes.MapHttpRoute(
  name: "ApiByAction",
  routeTemplate: "api/products/GetListOfStudents/{username}/{password}",
  defaults: new { controller = "products", action = "GetListOfStudents" }
);

Thanks everyone for your help!

谢谢大家的帮助!

#3


6  

Your GetListOfStudents action requires two parameters, username and password. Yet, the route definition contains neither specification in the route template where the values for those parameters should come from, nor specification for those parameter defaults in the defaults: parameter definition.

GetListOfStudents操作需要两个参数:用户名和密码。然而,路由定义既不包含路由模板中应该包含这些参数的值的规范,也不包含默认参数的规范:参数定义。

So when request comes in, routing is able to find your controller, but it is unable to find the action that it can call with the request and route context that it has because it has no information for the username and password parameters.

所以当请求进来时,路由能够找到你的控制器,但是它不能找到它可以用请求和路由上下文调用的动作,因为它没有用户名和密码参数的信息。

#4


3  

The most important is: ASP.Net's mvc not only seek action by name, also it will check method's signature, only the method is non-static, name matches and parameters matches, the action will be executed.

最重要的是:ASP。Net的mvc不仅要通过名称来查找操作,还会检查方法的签名,只有方法是非静态的,名称匹配和参数匹配,操作将被执行。

for your case, there are two ways to correct it. one way is declare default value, mvc will use default value is no such parameters

对于你的情况,有两种方法可以纠正它。一种方法是声明默认值,mvc使用默认值是没有这样的参数的

public List<Structures.StudentInfo> GetListOfStudents(string Username = null, string Password = null)
{
    List<Structures.StudentInfo> si = StudentFunctions.GetListOfStudents(Username, Password);
    return si;
}

the second way is use override

第二种方法是使用override

public List<Structures.StudentInfo> GetListOfStudents()
{
   return GetListOfStudents(null, null);
}

public List<Structures.StudentInfo> GetListOfStudents(string Username, string Password)
{
    List<Structures.StudentInfo> si = StudentFunctions.GetListOfStudents(Username, Password);
    return si;
}

#5


0  

I had this problem and solved it by including the verb as part of the action (i.e. GetThis, GetThat) and manually creating routes. I was attempting to create routes using attributes, but that did not work. This SO question may be the answer as to why the attributes aren't working, I haven't gotten that straightened out yet. As an additional note for anyone else having the same problem, when debugging it locally, IE was crashing when the "no action found" xml was returned. Once I gave up and switched to Chrome, the message detail was returned, and it was obvious that my controller at least was being found, it was just a matter of getting the action to work...

我有这个问题,并通过将动词作为动作的一部分(即GetThis, GetThat)和手动创建路由来解决它。我试图使用属性创建路由,但这行不通。这个问题可能是为什么属性不工作的答案,我还没弄清楚。对于任何有同样问题的人来说,当在本地调试IE时,当返回“没有找到操作”xml时,IE会崩溃。一旦我放弃并切换到Chrome,消息细节被返回,很明显我的控制器至少被找到了,这仅仅是让动作开始工作的问题……

#6


0  

If you want to call GetListOfStudents method without parameter you must set default value for parameter. such as GetListOfStudents(string Username=null, string Password=null) Otherwise you must call method with Parameters. GET http://localhost:8080/api/Products/GetListOfStudents/Username/Password

如果要调用没有参数的GetListOfStudents方法,必须为参数设置默认值。例如GetListOfStudents(string Username=null, string Password=null),否则必须使用参数调用方法。http://localhost:8080 / api /产品/ GetListOfStudents /用户名/密码

#7


-2  

When sending, encode the password with base64. Then when you about to use it decode it.

发送时,用base64对密码进行编码。然后当你准备用它解码它。

byte[] numArray = Convert.FromBase64String(password);
string Pass = Encoding.UTF8.GetString(numArray); 

List<Structures.StudentInfo> si = StudentFunctions.GetListOfStudents(Username, Pass);

Works fine for me.

对我来说很不错。