Have Microsoft changed how ASP.NET MVC deals with duplicate action method names?

Posted by Jason Evans on Stack Overflow See other posts from Stack Overflow or by Jason Evans
Published on 2012-12-14T18:09:00Z Indexed on 2012/12/15 17:04 UTC
Read the original article Hit count: 186

Filed under:

I might be missing something here, but in ASP.NET MVC 4, I can't get the following to work.

Given the following controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(string order1, string order2)
    {
        return null;
    }
}

and it's view:

@{
    ViewBag.Title = "Home";
}

@using (Html.BeginForm())
{
    @Html.TextBox("order1")<br />
    @Html.TextBox("order2")
    <input type="submit" value="Save"/>
}

When start the app, all I get is this:

The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index() on type ViewData.Controllers.HomeController System.Web.Mvc.ActionResult Index(System.String, System.String) on type ViewData.Controllers.HomeController

Now, in ASP.NET MVC 3 the above works fine, I just tried it, so what's changed in ASP.NET MVC 4 to break this?

OK there could be a chance that I'm doing something silly here, and not noticing it.

EDIT:

I notice that in the MVC 4 app, the Global.asax.cs file did not contain this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

which the MVC 3 app does, by default. So I added the above to the MVC 4 app but it fails with the same error. Note that the MVC 3 app does work fine with the above route. I'm passing the "order" data via the Request.Form.

EDIT: In the file RouteConfig.cs I can see RegisterRoutes is executed, with the following default route:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

I still get the original error, regards ambiguity between which Index() method to call.

© Stack Overflow or respective owner

Related posts about asp.net-mvc-4