1

I read here that I can't overload actions in MVC because of routing confusion I tried to overload Index() in HomeController and I got the exception as article said, but I noticed that microsoft has overloaded the actions in AccountController

public ActionResult Login(string returnUrl){}
public ActionResult Login(LoginModel model, string returnUrl){}

Please need clarification, thanks

Community
  • 1
  • 1
Abraham Josef
  • 653
  • 1
  • 8
  • 30
  • Check this link about overloading. http://stackoverflow.com/questions/7078543/c-sharp-mvc-3-action-overloading – RKS Apr 02 '14 at 08:26

5 Answers5

1

Microsoft has overloaded this by setting HttpGet and HttpPost. One for GET request and another for POST request. What about your code?

[HttpGet]
public ActionResult Login(string returnUrl){}

[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl){}
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
0

In AccountController first method works with GET method, second with POST one. It was realized by attribute [HttpGet] and [HttpPost].


Read more about get and post here.

0

Till today you cannot overload your controller's Action method with same name but different parameters.

The only possibility is to have two overload, and for that you need to set the method's property to HttpGet and HttpPost. For example

[HttpGet]
public ActionResult foo(string myString)
{
   return View();
}

[HttpPost]
public ActionResult foo(MyViewModelClass object)
{
   return View();
}

And regarding your confusion,

From general convention, first method should be of type Get which gets called when someone sends request to access that page.

Second method is called when user submits a form with his login details.

Cybercop
  • 8,475
  • 21
  • 75
  • 135
0

In addition to above answer we can add name attributes along with HTTPGET and HTTPPOST as

[HttpPost]
[ActionName("Edit")]
public ActionResult Edit_Post(some parameters)
{
//code over here
 }
After this we can call it as :- /MVC/EmployeeController/Edit/1
user1260967
  • 89
  • 1
  • 2
  • 14
0

Some definitions first:

  1. Overloading is a form of polymorphism, in particular an interface, in the sense of a class publicly visible part, related one.
  2. When we speak about inheritance we mean overriding.
  3. Action is a segment of a URL.

Back to your question... It is the ControllerActionInvoker which is responsible for finding the method to which an action is mapped. Given GET and POST we see polymorphic methods in a class mapped to the same action, but serving different HTTP methods (any action, that is, URL segment polymorphism here?!). And again yes, we may use ActionNameAttribute (MVC v5) to map an action to a class method, but again, this has nothing to do with any sort of polymorphism. Simply put, all that happens is in the ControllerActionInvoker and has nothing to do with overloading or overriding, and finally with any sort of polymorphism -- it is just mapping.

Conclusion. A simple question arises:

What in a string (a segment of a URL, 3.) relates to any one of the OOP definitions (1. and 2.) above? Or, in other words, can any of the OOP concepts above be transformed to a (part of a) string?

The fact that a transformation between URL segment, which happen to be called "action", and a class method exits does not imply that all we know about the first mechanically can be applied to the second and vice-verse. The question is misleading and its main intention is to confuse someone, not to test his/her knowledge (I suspect that this is an artificial interview questions, not a real one).

Alexander Christov
  • 9,625
  • 7
  • 43
  • 58