3

compiles but does not work:

public class ProductController : Controller
{
    public ActionResult List(int a)
    {
        return View();
    }

    public ActionResult List(int a, int b)
    {
        return View();
    }
 }

getting error:

The current request for action 'List' on controller type 'ProductController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult List(Int32) on type Shop.Controllers.ProductController System.Web.Mvc.ActionResult List(Int32, Int32) on type Shop.Controllers.ProductController

I'm interested in the reason why it was not possible to implement.

  • 4
    _ASP.NET MVC_ is just a web framework. It is not a programming language at all. I don't think there is a **support** issue.. – Soner Gönül Dec 25 '13 at 12:55
  • 2
    MVC is designed to route to an action, regardless of whether you actually pass the correct number of parameters or not. Any missing parameters become null, and any extras are just ignored. Because of that, it sees your overloaded methods as duplicate actions. – valverij Dec 25 '13 at 12:59

2 Answers2

2

MVC is a framework that handles routing http requests to controller actions. By default, when matching a route any missing parameters are passed as null and extra parameters are ignored. Therefore the routing engine would look at your route, match it to the default routing path and not be able to tell which it should call (it's ambiguous).

You can get around this by adding explicit routes to your route config calling out the expected parameters explicitly.

Wolfwyrd
  • 15,716
  • 5
  • 47
  • 67
0

Well, lets just pretend instead of ScottGu and Co writing ASP.NET MVC, you decided to write it.

You wrote your routing handler that handles the route to the Controller then to the Action

And now your MVC Handler starts executing

It checks the route, OK its controller = "Some" so, instantiate(using reflection) the SomeController class, then it sees action="List" call List method(Action).

Now tell us all how you can differentiate between List(int a) and List(int a, int b).

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78