测试时在运行时未找到重载的操作方法 [英] Overloaded Action Method not found at Runtime from test

查看:67
本文介绍了测试时在运行时未找到重载的操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种动作方法:

 public ActionResult Edit(int id)
 { 
 }
 public ActionResult Edit(Modelname model, string[] strParam)
 {
 }

我正在从单元测试中调用Edit(Modelname,string []).

And I am calling the Edit(Modelname,string[]) from the unit test.

var actionResult = controller.Edit(model, strParam);

代码在运行时编译,但是当我调试测试方法时,它给了我一个找不到的方法"MissingMethodException".我尝试注释Edit(int id)方法,然后进行调试,但还是一样.其他测试运行正常,任何帮助表示赞赏.

The code compiles at runtime , but when i debug the test method it gives me a method not found "MissingMethodException" . I tried commenting the Edit(int id) method and then debugging, still the same thing. Other tests are running fine, any help appreciated.

推荐答案

您在控制器中的操作方法有一个不明确的匹配项.尽管可以很好地进行编译,但是ASP.NET MVC无法确定在运行时使用哪种方法,并且会引发异常.您需要确保它们响应不同类型的HTTP请求或重命名其中之一.

You have an ambiguous match for action methods in your controller. While it will compile just fine, ASP.NET MVC can't decide which method to use at runtime and it throws an exception. You need to make sure they respond to different type of HTTP requests or rename one of them.

我不确定您提供的信息,但是如果第二种方法正在处理POST请求,则使用HttpPost过滤器可以解决此问题:

I can't be sure with the info you provided but if the second method is processing a POST request, using HttpPost filter will solve the problem:

public ActionResult Edit(int id)
{ 
}

[HttpPost]
public ActionResult Edit(Modelname model, string[] strParam)
{
}

如果不是这种情况,另一种解决方法是重命名.如果您有充分的理由不这样做,ASP.NET MVC提供了ActionName筛选器来覆盖ASP.NET MVC管道的方法名称:

If that's not the case, renaming is the other solution. If you have a good reason not to do that, ASP.NET MVC provides ActionName filter to override the method name for ASP.NET MVC pipeline:

public ActionResult Edit(int id)
{ 
}

[ActionName("EditModel")]
public ActionResult Edit(Modelname model, string[] strParam)
{
}

这将使 http://example.org/controller/EditModel 达到第二种方法.

This will make http://example.org/controller/EditModel hit the second method.

这篇关于测试时在运行时未找到重载的操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆