Overloaded Action Method not found at Runtime from test

孤者浪人 提交于 2020-01-15 22:12:36

问题


I have two action methods :

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

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

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

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.


回答1:


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.

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)
{
}

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)
{
}

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



来源:https://stackoverflow.com/questions/23838387/overloaded-action-method-not-found-at-runtime-from-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!