2
Reply

Can we overload methods of a controller in MVC?

Sahil Sharma

Sahil Sharma

Dec 18, 2014
2.4k
1

    Yes

    Mukesh Kumar
    September 05, 2017
    0

    Polymorphism is a part of C# programming while HTTP is a protocol. HTTP does not understand polymorphism it works on the concept's or URL and URL can only have unique name's. So HTTP does not implement polymorphism.And i know if you answer with the above argument MVC interviewer would still press that he wants to implement polymorphism , so how do we go about doing it.If you wish to keep polymorphism and also want the HTTP request to work you can decorate one of the methods with "ActionName" as shown in the below code.Hide Copy Code public class CustomerController : Controller{//// GET: /Customer/publicActionResultLoadCustomer(){ return Content("LoadCustomer");}[ActionName("LoadCustomerbyName")] publicActionResultLoadCustomer(string str){ return Content("LoadCustomer with a string");}} So now you can invoke with URL structure "Customer/LoadCustomer" the "LoadCustomer" action and with URL structure "Customer/LoadCustomerByName" the "LoadCustomer(string str)" will be invoked.

    monika garg
    April 28, 2016
    0