问题
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