ASP.NET MVC – How to fix ambiguous Action Methods errors and implement multiple Action Methods with same action name

ASP.NET MVC - How to fix ambiguous Action Methods errors and implement multiple Action Methods with same action name

If you’ve stumbled upon this post it probably means that you are dealing with a ASP.NET 4.x or ASP.NET Core MVC Controller containing two action methods that are conflicting, giving you the following error message:

The current request for action ‘ActionName’ on controller type ‘ControllerName’ is ambiguous between the following action methods: […]

This most likely happens because you have declared two (or more) Action Methods with the same action name, such as the following:

Unfortunately, MVC 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 HTTP VERB attribute, like – for example – for the former and for the latter:

This would indeed work… but what if we do want to work around it without having to change the HTTP VERB?

If we want to achieve such result, we have many alternatives:

  • Rename one of them: and will indeed (and obviously) work.
  • Move one of them to a different Controller.
  • Use the to declare an overload alias, such as .
  • Implement a custom attribute that will help the MVC request pipeline to understand with Action Method to use depending on the given parameters.

The latter option is by far the most interesting one: I got the idea from this StackOverflow thread a long time ago and I’m still using it – with some further enhancements, see below – in all my projects nowadays.

The first thing to do is to implement the attribute – we’ll call it – in the following way:

Then we can use it in the following way:

This will fix the issue and give you the chance to use multiple action methods with the same name, as long as they have different signatures.

Over the years, while working on many different MVC projects, I took the chance to further refine the to make it support multiple parameters and different kind of matches that would trigger it – all of the given parameters, any one of them or even none.

Here’s the updated class:

As we can see, it retains the old signature yet it also features an overload an a nice amount of configuration properties that can be used to further customize its behaviour.

Feel free to use it in your ASP.NET MVC projects!

 

 

RELATED POSTS

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan