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 - [HttpGet]  for the former and [HttpPost]  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: SomeMethod(...)  and SomeMethod2(...)  will indeed (and obviously) work.
  • Move one of them to a different Controller.
  • Use the System.Web.Mvc.ActionNameAttribute  to declare an overload alias, such as [ActionName("MyOverloadAliasName")] .
  • 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 RequireParameterAttribute  - 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 RequiredParameterAttribute  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!

 

 

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. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

3 Comments on “ASP.NET MVC - How to fix ambiguous Action Methods errors and implement multiple Action Methods with same action name”

  1. Tried it but get

    Value cannot be null.
    Parameter name: source
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: source

    Source Error:

    Line 41: case MatchMode.All:
    Line 42: default:
    Line 43: return (
    Line 44: (IncludeGET && ParameterNames.All(p => controllerContext.HttpContext.Request.QueryString.AllKeys.Contains(p)))
    Line 45: || (IncludePOST && ParameterNames.All(p => controllerContext.HttpContext.Request.Form.AllKeys.Contains(p)))

    1. It probably means that the ParameterNames property of the RequireParameterAttribute instance is NULL. Check that:

      1) the class constructor contains the following line:
      ParameterNames = parameterNames”;

      2) you’re passing a valid string to the attribute:
      [RequireParameter(“someParm”)]

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.