2

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.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • 1
    Did you change the default routing? If your default routing has two optional parameters then it won't be able to tell the difference between the two. – Erik Funkenbusch Dec 14 '12 at 18:11
  • If you want to overload the action name, you need to use `ActionName` attribute. Answered here: http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – Ray Cheng Dec 14 '12 at 18:19
  • @RayCheng - he's not overloading the action name in that way, he's using normal HttpPost method. – Erik Funkenbusch Dec 14 '12 at 18:24
  • Jason, You don't add that method to Global.asax, there is now a file in App_Start called RouteConfig.cs, and that method is called from global.asax – Erik Funkenbusch Dec 14 '12 at 18:26

1 Answers1

10

Because MVC4 ships with ASP.Net Web.API you can potentially reference two HttpPostAttribute (the same applies to the other attributes like HttpGet, etc.):

You have acidentally referenced System.Web.Http.HttpPostAttribute in your code. Change it to use the right attribute and it should work correctly:

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

    [System.Web.Mvc.HttpPost]
    public ActionResult Index(string order1, string order2)
    {
        return null;
    }
}
nemesv
  • 138,284
  • 16
  • 416
  • 359