3

Inside my asp.net mvc4 controller I have two action methods

public ActionResult Projects()

and

public ActionResult Projects(string s)

on debugging when I'm send string to this second method I'm getting error that current request is ambiguous between this two methods.

Why is that, since those have different method signature?

user2783193
  • 992
  • 1
  • 12
  • 37
  • possible duplicate of [ASP.NET MVC ambiguous action methods](http://stackoverflow.com/questions/1045316/asp-net-mvc-ambiguous-action-methods) – Tetsujin no Oni Oct 07 '13 at 15:49

3 Answers3

5

MVC it cannot support two actions with the same name... even with differents signatures. The only exception is when one of the actions is decorated with a different verb attribute, like [HttpPost].

If HttpPost is not suitable for you... you need to change the action name.

Just checking around, there are some alternatives to help you having 2 actions with the same name: https://stackoverflow.com/a/1045616/7720

Community
  • 1
  • 1
Romias
  • 13,783
  • 7
  • 56
  • 85
  • I usually solve this through routing. You can use standard routing to fix it by moving them to different controllers, or you can use stuff from attributerouting.net. You can always give them different names of course – Brian White Oct 08 '13 at 01:51
  • I'm not a big fan of Routing... but you are right. In my project, we have 60 controllers always using the standard route. – Romias Oct 08 '13 at 02:51
4

Decorate them with verbs, They have different function signature but most important they have same ActionName

[HttpGet]
public ActionResult Projects()


[HttpPost]
public ActionResult Projects(string s)

Or Change the action name of one of these mehod

Satpal
  • 132,252
  • 13
  • 159
  • 168
1

String is nullable type.

So your route will match both methods for optional.

In your case below are valid

/Home/Projects/

/Home/Projects/Business/

So decorate with different name

 public ActionResult ProjectBySting(string s)

or verb like [HttpPost]/[HttpGet]

Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120