-1

i wonder why ASP.NET MVC 3 is throwing me an exception

The current request for action 'PreviewProfile' on controller type 'EditController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult PreviewProfile() on type OSKus.Controllers.EditController System.Web.Mvc.ActionResult PreviewProfile(Int32) on type OSKus.Controllers.EditController

[HttpGet]
public ActionResult PreviewProfile()
{
    return View("PreviewProfile", user.GetPerson(User.Identity.Name));
}
[HttpGet]
public ActionResult PreviewProfile(int personId)
{
    if (personId == -1)
        return View("PreviewProfile", user.GetPerson(User.Identity.Name));
    return View("PreviewProfile", user.GetPerson(personId));
}
neubert
  • 15,947
  • 24
  • 120
  • 212
Puchacz
  • 1,987
  • 2
  • 24
  • 38
  • This seems to be a repeat of this questions: [Overload MVC Controller Methods][1] [1]: http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – Cubicle.Jockey Oct 14 '13 at 20:16

2 Answers2

2

You can't have two [HttpGet] actions with the same name even with different signatures.

[HttpGet]
public ActionResult PreviewProfile(int? personId)
{
    if (personId.HasValue)
        return View("PreviewProfile", user.GetPerson(personId));

    return View("PreviewProfile", user.GetPerson(User.Identity.Name));

}
Dmitry Efimenko
  • 10,973
  • 7
  • 62
  • 79
1

Because it can't distinguish between them in the routes defined.

But why not change the signature to:

public ActionResult PreviewProfile(int? personId)
{
    ìf(personId.HasValue)
    {
        // id supplied, do stuff and return view
    }

    // No id supplied
    // return default view
}
Syska
  • 1,150
  • 7
  • 26