1

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 Answers1

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.

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • I added the [HttpGet] and [HttpPost] parameters to both methods, still the same error.. I won't be able change the actionname as all projects follow the same standard. Why is it a problem at runtime i don't understand, both methods have different parameters and i'm calling the method correctly –  May 23 '14 at 22:08
  • @blackPearl One should have HttpGet, other should have HttpPost only. ASP.NET MVC uses method names to locate the action methods. When you have two methods with same name it can't decide which one to use. – Ufuk Hacıoğulları May 23 '14 at 22:22