1

i have home controller. my home controller has two index method one with no parameter and one with parameter. it is like

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View("index");
    }

    public ActionResult Index(int a)
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC! and Your Age is " + a;
        return View();
    }

i have define only two route entry in global.asax like

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

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

I want that when i type url like http://localhost:7221 or http://localhost:7221/home then index() method with no param of home controller should fire and

when i type http://localhost:7221/home/77 then index(int a) method with param of home controller should fire. but i am getting error why i type two kind of url i specified. the error message is The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index() on type BeginnerMVC.Controllers.HomeController System.Web.Mvc.ActionResult Index(Int32) on type BeginnerMVC.Controllers.HomeController

i am not being able to catch the error. what is wrong in my routing code. please help. thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

1

You can not use method overloading with actions in MVC. If you elaborate what you're trying to achieve, you would get advices on how to do that.

Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35
  • would please tell me why method overload is not possible in case of mvc action. – Thomas Aug 06 '12 at 16:42
  • @Thomas In short, "this behavior is by design". The way routing works and resolves action names prevents using overloads, because overloads, by definition, share same name, and action in routing is *only* a name, and thus has no way of finding right method. – Sergei Rogovtcev Aug 06 '12 at 16:51