2

I have coded a C# MVC5 Internet application and have a question about two ActionResult methods in the same controller.

I have two Index ActionResult methods as follows:

public async Task<ActionResult> Index()

public async Task<ActionResult> Index(int? mapCompanyId)

I am wanting to browse to either the Index() method or the Index(int? mapCompanyId) method, depending on if a value is specified for the mapCompanyId.

Currently, I am getting this error:

The current request for action 'Index' on controller type 'MapLocationController' is ambiguous between the following action methods:
System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult] Index() on type CanFindLocation.Controllers.MapLocationController
System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult] Index(System.Nullable`1[System.Int32]) on type CanFindLocation.Controllers.MapLocationController

I can rewrite my code so that there is only one Index ActionResult, but would rather have two if possible.

Is it possible to have two ActionResults with the same name, and depending on if a value is specified, the relevant ActionResult is executed. If so, is it easy to implement, or is it not worth the time?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Simon
  • 7,991
  • 21
  • 83
  • 163
  • MVC interprets a `int?` as a optional variable (how else can you set an int as null over GET?). So it is correct that you have two methods that match if you don't specify a `mapCompanyId`. The way to fix this is to simply make `mapCompanyId` a required variable, on your second method, by simply changing it to `public async Task Index(int mapCompanyId)`. MVC won't be confused which to use then. – Aron Jul 23 '14 at 04:30
  • @Aron Even if you make the integer *non*-nullable it will still result in the ambiguous exception. It's the fact that the framework can't determine which method to pick based on HTTP verb that causes the issue. – Justin Helgerson Jul 23 '14 at 04:34
  • @JustinHelgerson that sounds a lot like ASP.Net Web API and not MVC. MVC allows for filtering of by verb, but that's it. Whereas Web API works entirely on verbs. – Aron Jul 23 '14 at 04:37
  • possible duplicate of [Can you overload controller methods in ASP.Net MVC?](http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc) – Aron Jul 23 '14 at 04:42
  • @Aron I tested it before I commented. :) Here is the test code: http://pastebin.com/ZdYf3sYk. – Justin Helgerson Jul 23 '14 at 04:44

3 Answers3

2

This isn't possible since you're trying to do a GET request for each method. The ASP.NET MVC action selector doesn't know which method to pick.

If it makes sense and you're able you can use the HttpGet or HttpPost attributes to differentiate the method for each type of HTTP request. It doesn't sound like that makes sense in your case.

Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
1

You can assign different HTTP methods to the action overloads like this:

[HttpGet]
public async Task<ActionResult> Index()

[HttpPost]
public async Task<ActionResult> Index(int? mapCompanyId)

MVC runtime will then be able to pick a proper action based on the request HTTP method.

Alex Kiselev
  • 552
  • 2
  • 7
  • 22